Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7933419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:19:39+00:00 2026-06-03T21:19:39+00:00

I know this is very stupid question, but I wanted to clarify this. Let’s

  • 0

I know this is very stupid question, but I wanted to clarify this.

Let’s say I have one string vector looks like,

vector<int> vnTemp; // suppose this vector has {1,2,3,4,5}
vector<int>::iterator vn_it;

//Now, I want to print out only 1 to 4.
for(int i=0; i<4; ++i)
    cout << vnTemp[i] << endl;

Quite simple. but what should I do when I want to print out the equivalent result by using iterator? for exmample,

// .. continuing from the code above
for(vn_it = vnTemp.begin(); vn_it != vnTemp.end()-1; ++vn_it)
    cout << *it << endl;

Of course, vnTemp.end()-1 will lead an error since it’s pointer.

what is the equivalent for loop in this case?
and is there any performance difference when they both are compiled in optimized(-o) mode?


Edit:

I’ve just realized this actually works with vector.
The problem happened when I was using boost::tokenizer
the code is like this:

typedef boost::tokenizer<boost::char_separator<char> > tokenizer;

boost::char_separator<char> sep("_");
tokenizer::iterator tok_it;

tokenizer tokens(strLine, sep); //strLine is some string line
for(tok_it=tokens.begin(); tok_it != tokens.end(); ++tok_it){
... }

This was the original code, and error occurs when I try to say tokens.end()-1 in a for loop.

Is there any way that I can fix this problem?
Sorry about the ambiguity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:19:40+00:00Added an answer on June 3, 2026 at 9:19 pm

    boost::tokenizer only provides a forward iterator; which means that you can’t subtract from an tokenizer iterator. If you want your loop to avoid processing the last token you are going to have to “look-ahead” before you process the token you are on. Something like this

    tokenizer tokens(strLine, sep); //strLine is some string line
    tok_it = tokens.begin();
    if(tok_it!=tokens.end())
    {
        ++tok_it;
        for(auto last_tok = tokens.begin(); tok_it != tokens.end(); ++tok_it){
            // Process last_tok here
            . . .
           last_tok = tok_it;    
        }
    }
    

    Edit:
    An alternative approach would be to copy the tokens into a container with more flexible iterators:

    std::vector<std::string> resultingTokens(tokens.begin(), tokens.end());
    for(auto tok=resultingTokens.begin(); tok!=resultingTokens.end()-1; ++tok)
    {
        // whatever
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a very stupid question to ask but i have a
Right, I know this is a very stupid question, but I have no luck
I know this question is going to sound very stupid but here goes nonetheless.
this is a really stupid question but I have a deadline and I'm very
I know this might be a very stupid question but I am new to
This must be a very stupid newbie question, but I have spent my whole
I know this is a very broadly scoped question, but what do I need
I know this might be very easy to some,, I have a simple string
guys I know this question is very basic but I've met in few publications
Ok, I know this is a very basic question, but my head is swimming

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.