-
February 24th, 2011, 02:41 PM
#1
vector<string> help needed
Hello,
I have a "vector<string> stringVec"and here is what I want to do:
Read 10 messages from stringVec[0] to stringVec[9]. Then remove these and move stringVec[10] down to stringVec[0] and so forth. Also, if there is less than 10 message just read them all and empty the vector. Can someone help me with sample code to do this?
Thanks!
-
February 24th, 2011, 02:46 PM
#2
Re: vector<string> help needed
You'd be better off with a std:eque<std::string> since that has a pop_front() method.
You can do this with a vector, but it will be less efficient.
-
February 24th, 2011, 03:56 PM
#3
Re: vector<string> help needed
Originally Posted by Lindley
You'd be better off with a std: eque<std::string> since that has a pop_front() method.
Maybe it's not necessary to actually pop and move stuff. You could try simply std::copyying the data in and out (from the end to the begin of the vector).
I'm not sure whether the cost of copying std::string would be greater than processing the container itself. I guess it's about trying and experimenting...
-
February 24th, 2011, 08:46 PM
#4
Re: vector<string> help needed
Originally Posted by ltcmelo
You could try simply std::copyying the data in and out (from the end to the begin of the vector).
I doubt that would be any better than using the vector's erase() member to move stringVec[10] and all the following items down to stringVec[0], implementing what the OP described. (In a single (!) call to erase(), of course.)
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
February 25th, 2011, 04:27 AM
#5
Re: vector<string> help needed
Originally Posted by lab1
Read 10 messages from stringVec[0] to stringVec[9]. Then remove these and move stringVec[10] down to stringVec[0] and so forth. Also, if there is less than 10 message just read them all and empty the vector.
That sounds like a job for std::queue rather than vector.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|