今天在分析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()
标签:vector,示例,frame,list,uint8,fdata,AMR,data From: https://blog.51cto.com/u_15942605/6017164