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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:04:22+00:00 2026-05-13T06:04:22+00:00

If I have a std::string containing a comma-separated list of numbers, what’s the simplest

  • 0

If I have a std::string containing a comma-separated list of numbers, what’s the simplest way to parse out the numbers and put them in an integer array?

I don’t want to generalise this out into parsing anything else. Just a simple string of comma separated integer numbers such as “1,1,1,1,2,1,1,1,0”.

  • 1 1 Answer
  • 1 View
  • 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-13T06:04:22+00:00Added an answer on May 13, 2026 at 6:04 am

    Input one number at a time, and check whether the following character is ,. If so, discard it.

    #include <vector>
    #include <string>
    #include <sstream>
    #include <iostream>
    
    int main()
    {
        std::string str = "1,2,3,4,5,6";
        std::vector<int> vect;
    
        std::stringstream ss(str);
    
        for (int i; ss >> i;) {
            vect.push_back(i);    
            if (ss.peek() == ',')
                ss.ignore();
        }
    
        for (std::size_t i = 0; i < vect.size(); i++)
            std::cout << vect[i] << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a comma separated integers and I want to store them in std::vector<int>
I have an std::string which contains 3 numbers which are separated by spaces for
I'd like to test a std::string for containing numbers of any range e.g 5
Suppose I have a struct containing a std::string, like this: struct userdata{ int uid;
I have std::ostringstream out; out << name << len << data; std::string const value
we have std::string A with tcp://adr:port/ How to parse it into address std::string and
I have an std::string containing a command to be executed with execv, what is
When using CDT I would like to have std::string show up in the 'variable'
let's say I have std::map< std::string, std::string > m_someMap as a private member variable
I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as

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.