vector与list使用用法代码示例

简介: 将这些代码复制到文本文件中,文件命名韦testvector.c。然后用g++ testvector.c -o testvector 可以完成编译。

今天在分析amr解码时,发现用到了vector和list。考虑到这两种容器类使用的场景很多,想把他们的使用方法分享给读者。所以我单独抽离一部分单独编译,具体代码如下:


#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <list>
#include <unistd.h>
typedef unsigned char   uint8_t;
#define AMR_CMR_BITS 4
#define AMR_BE_TOC_BITS 6
#define FT_NODATA   15
enum AMR_CLASS{ AMR_CLASS_NONE, AMR = 1, AMR_WB };
enum AMR_MODE{ AMR_MODE_NONE, BE = 1, OA };    // BE: bindwidth-efficient, OA: octet-align 
typedef union be_toc_entry
{
    uint8_t v;
    struct {
        uint8_t q:1;
        uint8_t ft:4;
        uint8_t nl:1;   // not_last flag,  0: last frame, 1: not last
        uint8_t r:2;    // reserved
    };
}be_toc_entry_t;
class AmrFrameData
{
   public:
        AmrFrameData( uint8_t ft = FT_NODATA, uint8_t q = 1 ): frame_type(ft), quality(q){ }
   public:
        uint8_t frame_type;
        uint8_t quality;
        // data[0] is frame header for amr file, data[1] and remain bytes are core frame data
        std::vector<uint8_t> data;  
};
void testvector()
{
  std::vector<AmrFrameData> frame_data_list;
  std::list<AmrFrameData *> fdata_ptr_list;   // not used when construct
  frame_data_list.resize(1);
  be_toc_entry_t entry;
  std::vector<AmrFrameData>::iterator it = frame_data_list.begin();
  int l = 0;
  uint8_t v = 1;
  do
  {
        // multiple AMR frames in one RTP payload, this may happens rarely
        if ( it == frame_data_list.end() )
        {
            frame_data_list.resize( frame_data_list.size() + 1 );
            it = frame_data_list.end() - 1;
        }
        it->data.resize(10);
        it->data[0]=0;
        uint8_t *p = &it->data[1];
        for(int j=0;j<9;j++)
        {
          *p=0; 
          p++;
        }       
        it->frame_type = v;
        it->quality = v;
        it++;
        l++;
  } while ( l < 10 );
  std::vector<AmrFrameData>::iterator fdata_it;
  for ( fdata_it = frame_data_list.begin(); fdata_it != frame_data_list.end(); fdata_it++ )
        fdata_ptr_list.push_back(&*fdata_it);
 fdata_ptr_list.clear();
 frame_data_list.clear();
}
int main()
{
   char g = 0;
   do
   {
       testvector();
       //g = getchar();
       //printf('g=%c\n',g);
       //cout >> g;
       usleep(1000);
   }while (g!='q');
}


将这些代码复制到文本文件中,文件命名韦testvector.c。然后用g++ testvector.c -o testvector  可以完成编译。


vector的使用相关方法有:


std::vector<AmrFrameData>::iterator it = frame_data_list.begin();
frame_data_list.resize( frame_data_list.size() + 1 );
for ( fdata_it = frame_data_list.begin(); fdata_it != frame_data_list.end(); fdata_it++ )
        fdata_ptr_list.push_back(&*fdata_it);


list的使用相关方法有:


fdata_ptr_list.push_back(&*fdata_it);
fdata_ptr_list.clear()


相关文章
|
19天前
|
安全 C#
C# List基本用法
C# List基本用法
|
5月前
|
存储 Cloud Native Linux
C++ 什么时候使用 vector、list、以及 deque?
C++ 什么时候使用 vector、list、以及 deque?
|
5月前
|
存储 Cloud Native Linux
C++ 什么时候使用 vector、list、以及 deque?
C++ 什么时候使用 vector、list、以及 deque?
|
4天前
|
存储 缓存 编译器
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
|
1月前
|
编译器 C++ 容器
STL常用之vector,list,stack,queue,deque总结与对比
STL常用之vector,list,stack,queue,deque总结与对比
|
3月前
|
Java
java8 foreach用法list转map、map转list
java8 foreach用法list转map、map转list
|
4月前
|
C++ 容器
【C++】STL容器——探究List与Vector在使用sort函数排序的区别(14)
【C++】STL容器——探究List与Vector在使用sort函数排序的区别(14)
|
4月前
|
存储 C++ 容器
【C++】STL容器——list类的使用指南(含代码演示)(13)
【C++】STL容器——list类的使用指南(含代码演示)(13)
|
4月前
|
安全 Java
Java【代码分享 09】多线程处理List数据核心代码说明(下标越界、数据丢失及效率问题)
Java【代码分享 09】多线程处理List数据核心代码说明(下标越界、数据丢失及效率问题)
40 0
|
4月前
|
存储 C++ 容器
【STL】:list用法详解
【STL】:list用法详解
35 0