2012-04-01 12 views
0

私はバイナリデータでstd::vector<unsigned char>を持っている。読むデータ::ベクトル<unsigned char型>

私は単に特定のサイズで特定の位置に、このベクトルを読みたい

私はこのような機能になります。

myvector.read(PositionBegin, Size) 
myvector.read(1200,3) 

この機能は1203に、1200からデータを読み取ることができます。

C++にはこのような機能はありますか?

+0

私は、ベクターが本当にあなたがこの作業のために使用したい構造であるとは思いません。この[STLリファレンス](http://www.cplusplus.com/reference/)を見て、あなたのニーズに合ったものがあるかどうかを見てください。 – jpm

答えて

2

私はあなたが範囲外の別のstd :: vectorを望むと仮定しています...

このリンクは良い答えを提供しています。Best way to extract a subvector from a vector?

あなたの関数は次のようになります。

std::vector<unsigned char> readFromVector(const std::vector<unsigned char> &myVec, 
     unsigned start, unsigned len) 
{ 
    // Replaced T with unsigned char (you could/should templatize...) 
    std::vector<unsigned char>::const_iterator first = myVec.begin() + start; 
    std::vector<unsigned char>::const_iterator last = first + len; 
    std::vector<unsigned char> newVec(first, last); 
    return newVec; 
} 
0

質問には適切なタグを使用してください。

あなたのベクトルthrueシンプルイテレータ:あなたは範囲を使用したい場合

for (i=0; i<myvector.size(); i++) 
cout << " " << myvector.at(i); 
cout << endl; 

ので、あなたはちょうどあなたが出力にこの機能を使用する場合は、ループ制約

for (i=PositionBegin; i<PositionBegin+Size; i++) 
cout << " " << myvector.at(i); 
cout << endl; 

のためにあなたを設定する必要がありますこれを別のベクトル に変更する代わりに、新しいベクトルにプッシュする必要があります。

mynewvector.push_back(myvector.at(i)); 

あなたはこの関数を作っているとき、あなたはタイプとそれを作るために持っていることを忘れないでください:

return mynewvector; 

がベクトルをよく読んで:

vector<type> function() 

と終わりat:cplusplus

関連する問題