Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

zackriggle

STL Vectors

This topic is 5607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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  

Share this post


Link to post
Share on other sites
Advertisement

  
vector<int> v;
...

//erase the 5th element

v.erase(v.begin()+5);

//erase first occurance of 2

v.erase(find(v.begin(),v.end(),2));




[edited by - sjelkjd on March 15, 2003 8:03:05 PM]

Share this post


Link to post
Share on other sites
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!!

Share this post


Link to post
Share on other sites
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 --

Share this post


Link to post
Share on other sites
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 --

Share this post


Link to post
Share on other sites
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 ]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
IntelliSense is fairly braindead in VC6. Google for visualassist, I think, for a (commercial) solution.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!