This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 8 years ago.
How to break down a sentence of string type into words and store it in a vector of string type in c++?
Example
String str="my name";
Into
Vector word={" my","name"}
You can write a simple loop:
std::vector<std::string> words;
std::istringstream is("my name");
std::string word;
while (is >> word) {
// ...
words.push_back(word);
// ...
}
which in my opinion is good idea because you'll most likely need to do other things with those words apart the simple extraction of them. The body of the loop can be easily extended.
Related
This question already has answers here:
C++ : Creating an array with a size entered by the user
(3 answers)
Closed 3 years ago.
In One Of My Question , I Need To Get 'n' String And After I Get The Strings , I Should Process With Them:
{
int n=0;
cin>>n;
string user[n];
for(int i=0;i<n;i++)
{
cin>>user[n];
}
}
After I Get String How I Can Process With Them And Compare Them?
For Example If I Enter This Strings : "asdabdabmsd" and "ajksdasbgdjkabs" , How I Can Compare Specified Word In This Two String With Together?
First, get the length of two strings.After that use two loops for two strings and compare both strings character by character.
Firstly , correct the way you are storing strings. It should be cin>>user[i]
{
int n=0;
cin>>n;
std::vector <std::string> user(n);
for(int i=0;i<n;i++)
{
cin>>user[i]; // it should be 'i' not 'n'
}
}
Secondly, if you want to compare, say second letter of first string with the fourth letter of third string, use user[0][1] == user[2][3]
Means to get the ith letter of jth string, use user[j-1][i-1]
Instead of cin to get the input string, you need to use getline() (http://en.cppreference.com/w/cpp/string/basic_string/getline) or else if the input string contains white space the remaining part of the input string will not be ignored.
This question already has answers here:
Change string by index
(3 answers)
Closed 7 years ago.
string l;
cin>>l[0];
cout<<l;
input:a
output:
According to me the code must print the value of l[0] but why is there no output?
You need to do
cin>>l;
In your current state of code, when you try to access l[0] you are trying to access a memory location which may or may not be there. cin >> l[0] doesn't changes the size of the string which remains 0.
So when you try to do cout << l you are effectively printing an empty string.
Another alternative is
string s;
s.resize(1);
You have an empty string. There are no characters in it, including 0th character, you trying to read into. You need to actually add characters in string:
std::string l;
l.push_back('\0'); //Or any other character
std::cin >> l[0];
This question already has answers here:
how to check string start in C++
(11 answers)
Closed 7 years ago.
I have a vector composed of strings, some strings have single words, some have multiple words, some have numbers, etc. I have a code that deletes elements of the vector IF the entire string is one specific word ("event") that works perfectly:
for (int j = 0; j< myvec.size()-1; j++) {
if(myvec[j] == "<event>") { //erase all instances of "<event>"
myvec.erase(myvec.begin()+j);
}
}
However, now I need to delete a few elements in the vector that only START with a word (these all have differing junk after that first key word "wgt"
I have no idea how to get this working. I'm assuming it will be something similar to the above for/if loop, I just don't know how to make the if statement only look at the first word in the string.
Any ideas?
Thanks in advance!
The first line places string beginning with 'abc' at the end, and the second erases them from the vector.
auto end_it = std::remove_if(myvec.begin(), myvec.end(),
[](const string &str){
return str.find("abc") == 0 ;}) ;
myvec.erase(end_it, myvec.end()) ;
This question already has answers here:
Remove spaces from std::string in C++
(19 answers)
Closed 9 years ago.
I am trying to do something that I imagine is quite straightforward but I am new to C/C++ so it is proving a little bit tricky.
Essentially I am trying to remove a single whitespace from data contained within a .txt. Each piece of data is on a separate line:
01011 0
11100 1
00001 0
and so on. I have been able to count the number of lines, and the size of each string (including the whitespace) however I want to lose the whitespace located within the data.
My code for reading the data in (including whitespace is as follows):
std::ifstream myfile ("random.txt");
std::string str;
if(myfile.is_open())
{
while (std::getline(myfile, str))
{
i++;
Size = str.size();
data_input[i] = str;
line_num = i;
array_count = line_num * Size;
}
i = 0;
}
I have looked at various other posts but can't seem to find one that fits what I am trying to achieve. Any help would be appreciated.
str.erase(str.find(' '), 1);
Explanation:
The call to str.find returns the position (index) of the space.
The call to str.erase removes one character, starting at that position.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Splitting a string in C++
I am trying to read data from file where each line has 15 fields separated by commas and spaces. The data are not of a single type. Currently what I am doing is reading data line by line, and pass each line to an istringstream and between each read I do the following:
ins.ignore(25,','); //ins is the istringstream
I however don't like my method and would like a cleaner one. What would be a better way of doing it?. Also I would only like to use stl and no external libraries. Basically what I want is to tokenize each line using the comma as the delimiter.
Just use a custom manipulator:
std::istream& comma(std::istream& in) {
if ((in >> std::ws).get() != std::char_traits<char>::to_int_type(',')) {
in.setstate(std::ios_base::failbit);
}
return in;
}
...
in >> v0 >> comma >> v1 >> comma ...
Cleaner method (if I understand right) is just to read the comma into a dummy variable
char comma;
ins >> comma;
This will skip any whitespace and then read the comma, which you can then ignore.