STL Vectors

Started by
10 comments, last by zackriggle 21 years, 1 month ago
Alright, I''m not exactly sure this can be done, but here it goes: Can I remove a node from a vector (std::vector) that is NOT the last node in the list? ex.
  
vector <long> vLong;
vLong.push_back(13);
vLong.push_back(1);
vLong.push_back(9);
vLong.push_back(7);

vLong.remove_at_position(2); // Removes the 1

vLong.remove_by_value(13); // Removes the 13 at the beginning  
Advertisement

  vector<int> v;...//erase the 5th elementv.erase(v.begin()+5);//erase first occurance of 2v.erase(find(v.begin(),v.end(),2));  




[edited by - sjelkjd on March 15, 2003 8:03:05 PM]
Note that insertion/deletion at arbitrary points in a vector is an O(n) operation. If you''re going to do it a lot, consider a list, set, or map instead (depending on your exact requirements).

But... but that''s what HITLER would say!!
I have a question, and I am starting to NOT like MSVC. It says that (even after I have included the proper header files for vectors) the function "find()" was undeclared. Is there a seperate header file that I must include in order for this to work?

==================
My (soon-to-be) Cherished Cookie of Appreciation:

-- MattB - for WinSock advice --
#include <algorithm>

(And it''s not a MSVC thing ... this time.)
While I''m getting good help, I just want to check something...
I know a std::string cannot be passed to a function that wants a char* as an parameter. However, can i simply call the_string.data() and get a char* that is a copy of the data in the string? Also, is it null-terminated?

==================
My (soon-to-be) Cherished Cookie of Appreciation:

-- MattB - for WinSock advice --
quote:Original post by zackriggle
However, can i simply call the_string.data() and get a char* that is a copy of the data in the string? Also, is it null-terminated?


std::string::data() is not.
std::string::c_str() is.

Note that they return a const char* not a char*. Therefore the string cannot be modified. If you want a modifiable C string, use std::string::copy() (not null-terminated either.)

string str = "Foo.";char buf[1024];int last = str.copy( buf, 1024 );buf[last] = 0; 


Or something similar.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
One last question. MSVC seems to have a problem with showing me a vector object's members.

Example:
std::vector < int > a;
a. //<-- Does not pop up the little box

Example 2:
std::vector:: //<-- DOES pop up the little box

Nothing changes if I say "using namespace std", other than it screws everything up in fstream.h ...


Any ideas as to why this might happen? I don't think it has much to do with the std:: part, because std::string works just fine. YES, I did #include <vector>

[edited by - zackriggle on March 16, 2003 7:46:48 PM]
IntelliSense is fairly braindead in VC6. Google for visualassist, I think, for a (commercial) solution.

How appropriate. You fight like a cow.
Try declaring the vector this way
vector(int) a;
put it in greater than and less than brackets though. for some reason they didnt show up.

[edited by - Brad8383 on March 16, 2003 7:23:30 PM]

This topic is closed to new replies.

Advertisement