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

  • SEARCH
  • Home
  • 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 8001463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:05:55+00:00 2026-06-04T16:05:55+00:00

I am splitting a string into a vector of strings vector<string> tokens; stringstream strstm(str);

  • 0

I am splitting a string into a vector of strings

    vector<string> tokens;

    stringstream strstm(str);
    string item;
    while (getline(strstm, item, ' ')) {
        tokens.push_back(item);
    }

    token_idx = 0;

    cout << "size = " << tokens.size() << endl;

    for (unsigned int i = 0; i < tokens.size(); i++)
    {
        cout << tokens[i] << "[" << i << "]" << endl;
    } 

The split is successful, and the size() and its elements is what I like it to be. However the last token seems to act strangely when I try to get its value.

string Lexer::consume() {
    if (hasValue()) {
        token_idx++;
        cout << "consumed " << tokens[token_idx-1] << " tokens = " << token_idx -1 << endl;
        return tokens[token_idx-1];
    }
    cout << "didn't consume, token_idx = " << token_idx << endl;
    return "null";
}

hasVal is like this

bool Lexer::hasValue() {
    if ( token_idx < tokens.size()) {
        return true;
    } else {
        return false;
    }
}

if i have an input string like such 1 + 2 * 3 the expected output from my program should be (+1(*23)), however I am getting a segmentation error.

size = 5
1[0]
+[1]
2[2]
*[3]
3[4]
consumed 1 tokens = 0
consumed + tokens = 1
consumed 2 tokens = 2
consumed * tokens = 3
consumed 3 tokens = 4
Segmentation fault (core dumped)

But if i change the has value check to ( token_idx < tokens.size() -1 ), the program will return (+1 (*2 null))

size = 5
1[0]
+[1]
2[2]
*[3]
3[4]
consumed 1 tokens = 0
consumed + tokens = 1
consumed 2 tokens = 2
consumed * tokens = 3
didn't consume, token_idx = 4
(+1 (*2 null))

So I’m wondering if there’s a end of line after the 3 when splitting the way that I did or is there some other factors contributing to this behaviour? I am quite certain I am not going out of bounds for the vector though.

  • 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-04T16:05:56+00:00Added an answer on June 4, 2026 at 4:05 pm

    I think the real incriminated code generating the error is not showed her but since I can sense the way you are manipulating indice… there is no mistery that you have done an error accessing past the end on your token list, in addition whith an error prone design, that’s all.

    if (hasValue()) { // has value is useless to me
        token_idx++;  // why incrementing this here ?
    
        cout << "consumed " << tokens[token_idx-1] << " tokens = " << token_idx -1 << endl;
    
        return tokens[token_idx-1];
    }
    

    change it to this:

    if ( token_idx < tokens.size() ) { 
        cout << "consumed " << tokens[token_idx] << " tokens = " << token_idx << endl;
    
        return tokens [ token_idx++ ];
    }
    

    Also read about recursive descent parsing, It’s realy simple and you will be a lot more informed on parsing, avoiding common pitfalls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening, I'm trying to splitting the parts of a german address string into
I have an Arduino that is processing a string by splitting it into an
I want to split a string into each single character. Eg: Splitting : Geeta
How would I go on splitting a string into pairs of letter in VB?
Is there any function for splitting a string into chunks based on a separator?
I'm having an issue splitting a string array into a List/ArrayList. I realise this
I'm splitting a string into an array, then I want to remove the white
Let's say we have a long string we wish to split into strings 64
Possible Duplicate: Splitting a string into a list in python I'd like to take
Possible Duplicate: Splitting a string into chunks of a certain size Is there anyway

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.