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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:05:45+00:00 2026-06-14T22:05:45+00:00

I have two strings that look as follows: string text1 = you,are,good; string text2

  • 0

I have two strings that look as follows:

string text1 = "you,are,good";
string text2 = "1,2,3,4,5";
stringstream t1(text1);
stringstream t2(text2);

I’m using the following code to to parse it as comma separated data

template <typename T>
std::istream &operator>>(std::istream &is, Array<T> &t)
{
    T i;
    while (is >> i)
    {
        t.push_back(i);

        if (is.peek() == ',')
            is.ignore();
    }
    return is;
}

where “is” is t1 or t2. This separates text2 but fails with text1. Could you guys please help me with that and tell me why it doesn’t work with strings?
I need a general code that would parse strings and numbers.

Thanks for any efforts 🙂

  • 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-14T22:05:45+00:00Added an answer on June 14, 2026 at 10:05 pm

    The istream >> operator when applied to a string discards the eventual initial spaces and reads up to the first “space”.

    It works the same for whatever type (including int). It works in your code because at the ‘,’ the “int reader” fails, and assumes the following is something else.

    The simplest way to read comma separated strings is using the std::getline function, giving a ',' as a separator.

    In your case, your template function

    template <typename T>
    std::istream &operator>>(std::istream &is, Array<T> &t)
    { ...... }
    

    remains valid, but requires a specialization

    std::istream &operator>>(std::istream &is, Array<std::string> &t)
    {
        std::string r;
        while(std::getline(is,r,','))
            t.push_back(r);
        return is;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic program that compares two strings : #include <string> #include <iostream>
I have two string variables which are both file paths. The code that worked
I have several strings that look like the following: +H124005992014011/1527399999999I05Z +H7039700000001/$99999999I051 +K122005962050171/120911234C117 I need
If I have two strings that are identical in value, is it guaranteed that
Lets say that I have two strings: The System is in halt state. The
Let's say I have two strings like this: XABY XBAY A simple regex that
I have two arrays, @names and @employees , that are filled with strings that
I have two functions that return simple strings. Both are registered. $.views.helpers({ parseDate: function
I have a problem to create a preprocessor macro function, that concatenates two Strings
I have two functions that are supposed to encrypt and decrypt a string but

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.