How to read vector of string with spaces in it. Ex- “A B 5-2” “B A 3-2” …
I usually just read the whole line and manually write a function that splits it into a vector. For example in your case, I’d split after encountering the hyphen symbol and the last digit after it. So I just run a for loop with a bool for this, here’s an example of such code:
#include <bits/stdc++.h>
using namespace std;
vector<string> string_vector;
void split_input(string s) {
bool seen_hyphen = 0;
string cur = "";
for (int i = 0; i < s.length(); i++) {
if (s[i] == '-') {
seen_hyphen = 1;
cur.push_back(s[i]);
} else if (seen_hyphen && (s[i] == ' ')) {
string_vector.push_back(cur);
cur = ""; seen_hyphen = 0;
} else {
cur.push_back(s[i]);
}
}
string_vector.push_back(cur);
}
int main() {
string s;
getline(cin, s);
split_input(s);
for (auto it : string_vector) {
cout << it << ' ';
}
cout << '\n';
}