Jim_Moriarty_'s blog

By Jim_Moriarty_, history, 4 years ago, In English

Firstly we have string list then we have empty line then we have another string list. I want both list of strings to be stored in separate string vectors. How do I take the input.

Eg.

perfect rhyme crime time

crime rhyme

How do I take input here?

  • Vote: I like it
  • -3
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Jim_Moriarty_ (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it +4 Vote: I do not like it

Assume distinct words are on distinct lines. Do not consider one group as single string.

»
4 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

You can do it with string stream tokenizer by tokenizing the string according to any character. here's sample code for that, i tokenized the string by white space character ' '.

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define int long long int
using namespace std;
int32_t main(){
    #ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    string s,tok;
    while(t--){
        getline(std::cin,s);
        stringstream tokenizer(s);
        while(getline(tokenizer,tok,' ')){
            // if(tok=="not"){

            //     f=1;
            //     break;
            // }
            cout << tok << endl;
        }
    }
}

Here's cakewalk problem for stream tokenizer

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Hi, you can use getline function. Here is more information: getline (string) — C++ Reference

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it
vector<string> list[2];
string s;
getline(cin, s); // 1st line
string word;
s += ' ';
for(int i = 0; i < s.size(); i++){
	if(s[i] == ' '){
		list[0].push_back(word);
		word.clear();
	}else word += s[i];
}
getline(cin, s); // waster line
getline(cin, s); // 2nd line
s += ' ';
for(int i = 0; i < s.size(); i++){
	if(s[i] == ' '){
		list[1].push_back(word);
		word.clear();
	}else word += s[i];
}