Sunday 15 January 2012

c++ - How to modify a vector member's value? -



c++ - How to modify a vector member's value? -

i trying utilize vector svec store string values. when compiling @ dev c++ 5.6.1, compiler reported error of "no match operator=". why there error , how prepare it? thanks.

class="lang-cpp prettyprint-override"> #include<vector> #include<iostream> #include<string> using namespace std; int main() { vector<string> svec[100]; (int = 0; < 100; ++i) { svec[i] = "abc"; } homecoming 0; }

[error] no match 'operator=' (operand types 'std::vector >' , 'const char [4]')

edit: problem in vector svec[100]; things go after changing vector svec(100);

edit2: i'm curious compiler regards next declaration. svec still declared vector?

class="lang-cpp prettyprint-override">vector<string> svec[100];

vector<string> svec[100]; creating array 100 elements of std::vector<std::string>. that's valid syntax although it's rather odd thing do.

in current form, svec[i] hence refers std::vector<std::string>. assigning "abc" not possible. that's why error.

what mean write vector<string> svec(100);. calling 1 of vector constructors pre-sizes vector have 100 elements.

it happens std::vector overloads [] operator give element access. (a rather convenient analogue pure arrays.) if create change, svec[i] refer string can set in way do.

c++ vector operators

No comments:

Post a Comment