Archived

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

zackriggle

STL Vectors

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

  
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
Okay I guess that WASN''T my last question. The whole STL thing is getting on my nerves more than pointers . Anyways, here''s the question:

I understand that std::find() can find a certain element in a vector or other STL container... but, if I have a vector of an object, can I search through the vector for an object with a member that is a certain value?

Example

  
class MyClass
{
public:
int a;
};

void main()
{
std::vector<MyClass> MyVector;
MyClass mc;
mc.a = 15;
MyVector.push_back(mc);
mc.a = 7;
MyVector.push_back(mc);
mc.a = 15144;
MyVector.push_back(mc);
mc.a = 12457;
MyVector.push_back(mc);
std::vector<MyClass>::iterator i;
i = Search(MyVector,a,7); // <-- Searches for an element whose

// member ''a'' is set to 7...

// And use A to do whatever is neccesary...

};

Share this post


Link to post
Share on other sites
If you want to do this through generic programming (using STL algorithms and suchlike, you will want to look into std::find_if. Just make a predicate functor that checks member equality, and feed it in to find_if. This is the "right" STL way to do it. But if you're like me, or most others, you'll just iterate through the freakin' container. With few exceptions, the STL algorithms are not appreciably faster than manual iteration, and some have a habit of incurring a lot of calling overhead.

Oh, if you haven't found it yet: the best STL docs are at http://www.sgi.com/tech/stl/ (memorize this URL).


How appropriate. You fight like a cow.

[edited by - sneftel on March 17, 2003 2:04:35 AM]

Share this post


Link to post
Share on other sites