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 7039493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:48:18+00:00 2026-05-28T01:48:18+00:00

I was writing a code to tokenize a string wrt delimeters ,. void Tokenize(const

  • 0

I was writing a code to tokenize a string wrt delimeters “,”.

    void Tokenize(const string& str, vector<string>& tokens, const string& delimeters)
    {
      // Skip delimiters at beginning.
      string::size_type lastPos = str.find_first_not_of(delimiters, 0);
      // Find first "non-delimiter".
      string::size_type pos     = str.find_first_of(delimiters, lastPos);

      while (string::npos != pos || string::npos != lastPos)
      {
         // Found a token, add it to the vector.
         tokens.push_back(str.substr(lastPos, pos - lastPos));
         // Skip delimiters.  Note the "not_of"
         lastPos = str.find_first_not_of(delimiters, pos);
         // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
      }
    }

    int main()
    {
       string str;
       int test_case;
       cin>>test_case;
       while(test_case--)
       {
           vector<string> tokens;
           getline(cin, str);
           Tokenize(str, tokens, ",");
           // Parsing the input string 
           cout<<tokens[0]<<endl;
       }
       return 0;
    }

It gives segmentation fault on running. When I debugged it the line

    cout<<tokens[0]<<endl 

was the cause of problem.I can’t understand why because at cplusplus.com it uses the [ ] operartors for accesing the values of vectors

  • 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-05-28T01:48:19+00:00Added an answer on May 28, 2026 at 1:48 am
    cin>>test_case; // this leaves a newline in the input buffer
    while(test_case--)
    {
        vector<string> tokens;
        getline(cin, str); // already found newline
        Tokenize(str, tokens, ",");  // passing empty string
    

    Without looking at your Tokenize function, I would guess that an empty string results in an empty vector, which means when you print tokens[0], that element does not actually exist. You need to make sure your input buffer is empty before you call getline. You could put a call to cin.ignore() right after your number input, for example.

    You could also forgo operator>>, and only use getline. Then do string to number conversions with your favorite method.

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

Sidebar

Related Questions

While writing code in a file that would comprise of PHP, HTML, CSS &
Writing code with implicit cast generates CS0266 . Dictionary<Int32, string[]> d1 = new Dictionary<int,
When writing code like: public class TestBasic { public static void print(Object o){ System.out.println(o);
While writing code, it is pretty common to request a page with an appended
When writing code in an Eclipse project, I'm usually quite messy and undisciplined in
I am currently writing code in C++ to find all possible permutations of 6
I'm writing code to use UIImagePickerController. Corey previously posted some nice sample code on
I am writing code in Java where I branch off based on whether a
I find myself writing code such as: foreach($array as $key => $value) { switch($key)
I'm writing code to read in a 7x15 block of text in a file

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.