|
Post by Tampaboy19N on Oct 5, 2009 8:21:20 GMT -5
I create one vector... how to print the element but when the element's value is 20 erase it, and continue to print the element in the vector?
vector<int> v; for (int i=1; i<6; i++) { v.push_back(i*10); }
|
|
|
Post by darckonquest on Oct 5, 2009 13:36:33 GMT -5
I create one vector... how to print the element but when the element's value is 20 erase it, and continue to print the element in the vector? vector<int> v; for (int i=1; i<6; i++) { v.push_back(i*10); } Vectors work about the same way as arrays, but they're resizable at runtime : for(int i = 0; i < v.size(); i++) { if(v == 20) { v = null; } else { cout << "current position = " << v << endl; } }
-darc
|
|
|
Post by Desired Apathy on Oct 5, 2009 15:46:58 GMT -5
I want to learn C++
|
|
|
Post by Mr. Tuxedo on Oct 5, 2009 20:15:09 GMT -5
vector<int> v; for( vector<int>::iterator iter = v.begin(); iter != v.end(); ) { if( *iter == 20 ) { iter = v.erase(iter); } else { ++iter; } }
|
|