473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Iterate Vector of Strings Backwards

94 64KB
Hello, everyone. I have a variable called listOfTokens, of type std::vector< std::string >, and I am trying to iterate through the elements backwards with the following code:

Expand|Select|Wrap|Line Numbers
  1. std::vector< std::string >::iterator it;
  2. std::string currentToken;
  3.  
  4. for ( it = listOfTokens.end() - 1; it != listOfTokens.begin() - 1 ; it-- ){
  5. currentToken = *it;
  6. //...
  7. }
However, I'm getting a runtime error. What am I doing wrong? Thaks in advance.
Feb 6 '14 #1

✓ answered by Banfa

Your using a forward iterator and calling the forward begin and end methods, if your code worked you would not include the first item in the vector in your loop.

Try using a reverse iterator with reverse begin and end

Expand|Select|Wrap|Line Numbers
  1. std::vector<std::string>::reverse_iterator it;
  2. std::string currentToken;
  3.  
  4. for ( it = listOfTokens.rbegin(); it != listOfTokens.rend(); ++it ){
  5.   currentToken = *it;
  6.   //...
  7. }

3 2073
Banfa
9,065 Expert Mod 8TB
Your using a forward iterator and calling the forward begin and end methods, if your code worked you would not include the first item in the vector in your loop.

Try using a reverse iterator with reverse begin and end

Expand|Select|Wrap|Line Numbers
  1. std::vector<std::string>::reverse_iterator it;
  2. std::string currentToken;
  3.  
  4. for ( it = listOfTokens.rbegin(); it != listOfTokens.rend(); ++it ){
  5.   currentToken = *it;
  6.   //...
  7. }
Feb 6 '14 #2
stdq
94 64KB
Thank you! I did that, but I'm still getting a runtime error. I guess the problem lies somewhere else in my program. Thanks, though!
Feb 6 '14 #3
Banfa
9,065 Expert Mod 8TB
Try runni9ng you program in a symbolic debugger, like gdb or debug mode of MSVC++, it should stop when you get the error and allow you to examine the call stack which might indicate what the problem is.
Feb 7 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Jay | last post by:
I have a very large text file (being read by a CGI script on a web server), and I get memory errors when I try to read the whole file into a list of strings. The problem is, I want to read the file...
20
by: Hanzo | last post by:
I'm iterating over a vector of base class pointers and deleting those which meet a certain criteria...i'm using pretty text-book code for the particular delete/erasure (it's straight out of Myers'...
15
by: SK | last post by:
Hey folks, I am searching for a string (say "ABC") backwards in a file. First I seek to the end. Then I try to make a check like - do { file.clear (); file.get(c); file.seekg(-2,...
8
by: Jim Langston | last post by:
I have a class I designed that stores text chat in a std::vector<sd::string>. This class has a few methods to retrieve these strings to be displayed on the screen. void ResetRead( bool Reverse,...
10
by: Aris | last post by:
A few days ago I asked from you how to join a string like "file" A number that change values from 1 to 5 and another string like ".txt" to have a result like "file1.txt","file2.txt" and so on ...
9
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
3
by: edu.mvk | last post by:
Hi, i have the following code which updates the vector of strings( p_vector ) each time it goes into the loop for the first iteration we have only one string in the vector. But in the while loop...
4
by: Rakesh Kumar | last post by:
Hi All - In a project of mine - I was trying to scale down the actual issue to the following piece of code. I need to allocate an array of strings and reserve the individual string to a particular...
4
by: fishirboy | last post by:
I want to have it so that when i ask for the person witch item they want to drop on the ground it goes into another vector that i can pick back up the item if they want it back and erase when they...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.