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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:53:40+00:00 2026-06-12T01:53:40+00:00

What is the easiest way code parsing string input like the following: WORD WORD2

  • 0

What is the easiest way code parsing string input like the following:

WORD WORD2
WORD3 WORD4

WORD5
WORD6

That is, an unknown number of pairs of words followed by a blank line followed by an unknown number of words one-per line. I want to put the first group of these words into a map, and the second list of words into a vector.

Using getline has problems discovering when the first group of paired words terminates.

  • 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-12T01:53:42+00:00Added an answer on June 12, 2026 at 1:53 am

    A simple matter of using getline and some containers:

    #include <string>
    #include <sstream>
    #include <iostream>
    #include <vector>
    #include <map>
    
    std::map<std::string, std::string> m;
    std::vector<std::string>           v;
    
    for (std::string line; std::getline(std::cin, line); )
    {
        if (line.empty()) { break; }
    
        std::string x, y;
        std::istringstream iss(line);
    
        if (!(iss >> x >> y >> std::ws)) { /* fatal error */ }
    
        m[x] = y;
    }   
    
    for (std::string line; std::getline(std::cin, line); )
    {
        v.push_back(std::move(line));
    }
    
    std::cout << "We read " << m.size() << " pairs and " << v.size() << " words.\n";
    

    If you want to make sure that the first part of the file consists precisely of pairs of words with a single region of whitespace in between, you can strengthen the condition to this:

    if (!(iss >> x >> y >> std::ws) || iss.get() != EOF) { /* fatal error */ }
    

    Similarly, you can add a check that the second part of the file contains no whitespace if you like. Finally, you could use insert() for the map to check that there are no duplicate keys; in C++11, you should be able to say m.emplace(std::move(x), std::move(y));. And an unordered_map is probably a lot more efficient with strings.

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

Sidebar

Related Questions

I have the following code, what is the easiest way to check if $data
I would like to know the easiest way to wait for code to finish
The easiest way to explain this problem is to show you some code: Public
What would be the easiest way of using commands in the code to programatically
What's the easiest way to search a div for a text string using jquery?
What would be the easiest way to convert the following key->value object array to
What would be the easiest way to update a HTML tag with the following
which is the easiest way to generate EAN 13 code? I want to implement
What is the best/easiest way to configure logging for code kept in the lib
What is the easiest way to create code-behind (webforms) event handlers for say a

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.