push_back all contents in vector to combine them as the first element of vector
up vote
0
down vote
favorite
I'm trying to parse a string with spaces into several strings and store them into a list, which consists of strings without any space. I do not know how long the input of the string will me and I have the following code:
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
vector<string> myWords;
vector<char> myBuffer;
int main() {
string mySentence;
getline(cin, mySentence);
int j = 0;
for (int i = 0; i < mySentence.length(); i++) {
if (mySentence[i] != ' ') myBuffer.push_back(mySentence[i]);
else {
myWords.push_back(myBuffer);
myBuffer.clear();
j++;
}
}
return 0;
}
The error in which I'm getting is at myWords.push_back(myBuffer);
. How do I get around this?
c++ string
|
show 10 more comments
up vote
0
down vote
favorite
I'm trying to parse a string with spaces into several strings and store them into a list, which consists of strings without any space. I do not know how long the input of the string will me and I have the following code:
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
vector<string> myWords;
vector<char> myBuffer;
int main() {
string mySentence;
getline(cin, mySentence);
int j = 0;
for (int i = 0; i < mySentence.length(); i++) {
if (mySentence[i] != ' ') myBuffer.push_back(mySentence[i]);
else {
myWords.push_back(myBuffer);
myBuffer.clear();
j++;
}
}
return 0;
}
The error in which I'm getting is at myWords.push_back(myBuffer);
. How do I get around this?
c++ string
What does you debug output say that you have you've found? I don't get what you wan't to achieve.
– Ted Lyngmo
8 hours ago
Also, "The error" .. would be good to know.
– Ted Lyngmo
8 hours ago
2
#include <bits/stdc++.h>
-- Don't do this
– PaulMcKenzie
8 hours ago
1
I'm trying to parse a string with spaces into several strings and store them into a list -- This is much simpler if you used std::istringstream
– PaulMcKenzie
8 hours ago
1
@TedLyngmo The error says:error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'
.
– Wealthy Player
7 hours ago
|
show 10 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to parse a string with spaces into several strings and store them into a list, which consists of strings without any space. I do not know how long the input of the string will me and I have the following code:
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
vector<string> myWords;
vector<char> myBuffer;
int main() {
string mySentence;
getline(cin, mySentence);
int j = 0;
for (int i = 0; i < mySentence.length(); i++) {
if (mySentence[i] != ' ') myBuffer.push_back(mySentence[i]);
else {
myWords.push_back(myBuffer);
myBuffer.clear();
j++;
}
}
return 0;
}
The error in which I'm getting is at myWords.push_back(myBuffer);
. How do I get around this?
c++ string
I'm trying to parse a string with spaces into several strings and store them into a list, which consists of strings without any space. I do not know how long the input of the string will me and I have the following code:
#include <bits/stdc++.h>
#include <sstream>
using namespace std;
vector<string> myWords;
vector<char> myBuffer;
int main() {
string mySentence;
getline(cin, mySentence);
int j = 0;
for (int i = 0; i < mySentence.length(); i++) {
if (mySentence[i] != ' ') myBuffer.push_back(mySentence[i]);
else {
myWords.push_back(myBuffer);
myBuffer.clear();
j++;
}
}
return 0;
}
The error in which I'm getting is at myWords.push_back(myBuffer);
. How do I get around this?
c++ string
c++ string
asked 8 hours ago
Wealthy Player
1506
1506
What does you debug output say that you have you've found? I don't get what you wan't to achieve.
– Ted Lyngmo
8 hours ago
Also, "The error" .. would be good to know.
– Ted Lyngmo
8 hours ago
2
#include <bits/stdc++.h>
-- Don't do this
– PaulMcKenzie
8 hours ago
1
I'm trying to parse a string with spaces into several strings and store them into a list -- This is much simpler if you used std::istringstream
– PaulMcKenzie
8 hours ago
1
@TedLyngmo The error says:error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'
.
– Wealthy Player
7 hours ago
|
show 10 more comments
What does you debug output say that you have you've found? I don't get what you wan't to achieve.
– Ted Lyngmo
8 hours ago
Also, "The error" .. would be good to know.
– Ted Lyngmo
8 hours ago
2
#include <bits/stdc++.h>
-- Don't do this
– PaulMcKenzie
8 hours ago
1
I'm trying to parse a string with spaces into several strings and store them into a list -- This is much simpler if you used std::istringstream
– PaulMcKenzie
8 hours ago
1
@TedLyngmo The error says:error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'
.
– Wealthy Player
7 hours ago
What does you debug output say that you have you've found? I don't get what you wan't to achieve.
– Ted Lyngmo
8 hours ago
What does you debug output say that you have you've found? I don't get what you wan't to achieve.
– Ted Lyngmo
8 hours ago
Also, "The error" .. would be good to know.
– Ted Lyngmo
8 hours ago
Also, "The error" .. would be good to know.
– Ted Lyngmo
8 hours ago
2
2
#include <bits/stdc++.h>
-- Don't do this– PaulMcKenzie
8 hours ago
#include <bits/stdc++.h>
-- Don't do this– PaulMcKenzie
8 hours ago
1
1
I'm trying to parse a string with spaces into several strings and store them into a list -- This is much simpler if you used std::istringstream
– PaulMcKenzie
8 hours ago
I'm trying to parse a string with spaces into several strings and store them into a list -- This is much simpler if you used std::istringstream
– PaulMcKenzie
8 hours ago
1
1
@TedLyngmo The error says:
error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'
.– Wealthy Player
7 hours ago
@TedLyngmo The error says:
error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'
.– Wealthy Player
7 hours ago
|
show 10 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
The problem is that you are trying to push a std::vector<char>
where a std::string
is expected. So simply change the type of myBuffer
to a std::string
:
#include <iostream>
#include <string>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::string myBuffer;
for (int i = 0; i < mySentence.length(); i++) {
if (mySentence[i] != ' ')
myBuffer.push_back(mySentence[i]);
else {
myWords.push_back(myBuffer);
myBuffer.clear();
}
}
if (!myBuffer.empty()) {
myWords.push_back(myBuffer);
}
// use myWords as needed...
return 0;
}
That being said, using a std::istringstream
would be much simpler, as operator>>
reads whitespace-delimited values from a stream for you:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::string myBuffer;
std::istringstream iss(mySentence);
while (iss >> myBuffer) {
myWords.push_back(myBuffer);
}
// use myWords as needed...
return 0;
}
Alternatively, let the standard library handle the reading and pushing for you:
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::istringstream iss(mySentence);
std::copy(
std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(myWords)
);
// use myWords as needed...
return 0;
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
The problem is that you are trying to push a std::vector<char>
where a std::string
is expected. So simply change the type of myBuffer
to a std::string
:
#include <iostream>
#include <string>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::string myBuffer;
for (int i = 0; i < mySentence.length(); i++) {
if (mySentence[i] != ' ')
myBuffer.push_back(mySentence[i]);
else {
myWords.push_back(myBuffer);
myBuffer.clear();
}
}
if (!myBuffer.empty()) {
myWords.push_back(myBuffer);
}
// use myWords as needed...
return 0;
}
That being said, using a std::istringstream
would be much simpler, as operator>>
reads whitespace-delimited values from a stream for you:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::string myBuffer;
std::istringstream iss(mySentence);
while (iss >> myBuffer) {
myWords.push_back(myBuffer);
}
// use myWords as needed...
return 0;
}
Alternatively, let the standard library handle the reading and pushing for you:
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::istringstream iss(mySentence);
std::copy(
std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(myWords)
);
// use myWords as needed...
return 0;
}
add a comment |
up vote
0
down vote
The problem is that you are trying to push a std::vector<char>
where a std::string
is expected. So simply change the type of myBuffer
to a std::string
:
#include <iostream>
#include <string>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::string myBuffer;
for (int i = 0; i < mySentence.length(); i++) {
if (mySentence[i] != ' ')
myBuffer.push_back(mySentence[i]);
else {
myWords.push_back(myBuffer);
myBuffer.clear();
}
}
if (!myBuffer.empty()) {
myWords.push_back(myBuffer);
}
// use myWords as needed...
return 0;
}
That being said, using a std::istringstream
would be much simpler, as operator>>
reads whitespace-delimited values from a stream for you:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::string myBuffer;
std::istringstream iss(mySentence);
while (iss >> myBuffer) {
myWords.push_back(myBuffer);
}
// use myWords as needed...
return 0;
}
Alternatively, let the standard library handle the reading and pushing for you:
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::istringstream iss(mySentence);
std::copy(
std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(myWords)
);
// use myWords as needed...
return 0;
}
add a comment |
up vote
0
down vote
up vote
0
down vote
The problem is that you are trying to push a std::vector<char>
where a std::string
is expected. So simply change the type of myBuffer
to a std::string
:
#include <iostream>
#include <string>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::string myBuffer;
for (int i = 0; i < mySentence.length(); i++) {
if (mySentence[i] != ' ')
myBuffer.push_back(mySentence[i]);
else {
myWords.push_back(myBuffer);
myBuffer.clear();
}
}
if (!myBuffer.empty()) {
myWords.push_back(myBuffer);
}
// use myWords as needed...
return 0;
}
That being said, using a std::istringstream
would be much simpler, as operator>>
reads whitespace-delimited values from a stream for you:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::string myBuffer;
std::istringstream iss(mySentence);
while (iss >> myBuffer) {
myWords.push_back(myBuffer);
}
// use myWords as needed...
return 0;
}
Alternatively, let the standard library handle the reading and pushing for you:
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::istringstream iss(mySentence);
std::copy(
std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(myWords)
);
// use myWords as needed...
return 0;
}
The problem is that you are trying to push a std::vector<char>
where a std::string
is expected. So simply change the type of myBuffer
to a std::string
:
#include <iostream>
#include <string>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::string myBuffer;
for (int i = 0; i < mySentence.length(); i++) {
if (mySentence[i] != ' ')
myBuffer.push_back(mySentence[i]);
else {
myWords.push_back(myBuffer);
myBuffer.clear();
}
}
if (!myBuffer.empty()) {
myWords.push_back(myBuffer);
}
// use myWords as needed...
return 0;
}
That being said, using a std::istringstream
would be much simpler, as operator>>
reads whitespace-delimited values from a stream for you:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::string myBuffer;
std::istringstream iss(mySentence);
while (iss >> myBuffer) {
myWords.push_back(myBuffer);
}
// use myWords as needed...
return 0;
}
Alternatively, let the standard library handle the reading and pushing for you:
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
int main() {
std::string mySentence;
std::getline(std::cin, mySentence);
std::vector<std::string> myWords;
std::istringstream iss(mySentence);
std::copy(
std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(myWords)
);
// use myWords as needed...
return 0;
}
edited 5 hours ago
answered 5 hours ago
Remy Lebeau
326k18244430
326k18244430
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348309%2fpush-back-all-contents-in-vectorchar-to-combine-them-as-the-first-element-of-v%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What does you debug output say that you have you've found? I don't get what you wan't to achieve.
– Ted Lyngmo
8 hours ago
Also, "The error" .. would be good to know.
– Ted Lyngmo
8 hours ago
2
#include <bits/stdc++.h>
-- Don't do this– PaulMcKenzie
8 hours ago
1
I'm trying to parse a string with spaces into several strings and store them into a list -- This is much simpler if you used std::istringstream
– PaulMcKenzie
8 hours ago
1
@TedLyngmo The error says:
error: no matching function for call to 'std::vector<std::__cxx11::basic_string<char>::push_back(std::vector<char>&)'
.– Wealthy Player
7 hours ago