0

私はc ++にまったく慣れていません。

ベクトルのポインターが配列を指すようにする方法があるかどうか疑問に思っていました。

私のプログラムには、次のように、ベクトルの最初と最後の要素を指すベクトル反復子オブジェクトがあります。

vector<int>::iterator vb = vec.begin();
vector<int>::iterator ve = vec.end();

「result」という配列があります。

'vb' が 'result' の最初の要素を指し、've' が 'result' の最後の要素を指すようにしたいと考えています。

私はこれを試しました:

vb = &result;
int resultLen = result.size();
ve = &(result+resultLen);

しかし、私はこのエラーで終わった:

`error: no match for ‘operator=’ in ‘vb = & result’

次のようないくつかのバリエーションを試しました。

*vb = &result;
int resultLen = result.size();
*ve = &(result+resultLen);

それもうまくいきませんでした。

どんな助けでも大歓迎です、そして前もって感謝します!!

================================================== =========================== 更新。これは、私が書こうとしているプログラムの単純なバージョンです。

vector<int>::iterator vb = vec1.begin(); 
vector<int>::iterator ve = vec1.end();

int arr = {1,2,3}
int result [10];

while (True) {
    subtraction (vb, ve, arr, arr+5, result); 
// let's say the vector has {1,2,3,4,5}. I am subtracting array from vector like this: 12345 - 123 until it becomes less than 123. 
        /*I now need to update the vector from which I am subtracting the array to the result array.*/
        vb = &result; //points to the first element of array
        int resultLen = result.size();
        ve = &result+resultLen; //points to the last element of array
    }

ベクトルの更新は、私が問題を抱えているところです。

4

2 に答える 2

0

それは可能です、ここに私の単純化したあなたのサンプルコードがあります:

std::vector<int> vec1;

vec1.push_back(1);
vec1.push_back(2);
vec1.push_back(3);

std::vector<int>::iterator vb = vec1.begin();
std::vector<int>::iterator ve = vec1.end();

int result[] = {3,2,1};
*vb = *result; //points to the first element of array
int resultLen = sizeof(result)/sizeof(result[0]);
*ve = *(result+resultLen-1); //points to the last element of array

std::cout<<vec1[0]<< " " <<vec1[1] << " " <<vec1[2] << " " <<vec1[3];

結果は次のとおりです。

3 2 3 1

iterator::end にアクセスしていることを思い出してください。これは、最後の値を過ぎたことを意味します。

于 2013-07-16T13:02:27.217 に答える
0

resultはベクトルではないからです。vector の iterator を配列に割り当てることはできません。int型ベクトルにのみ割り当てることができます。

于 2013-07-07T10:06:22.743 に答える