Splitting a String into a Vector C++ [duplicate] - c++

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 9 years ago.
I have been working on the following piece of code; It opens an inputfile, reads the input, and now I wish to take that input (stored in a string) and split it by whitespaces; inputting each element into an integer vector.
I understand how to typecast. The problem is in the splitting. How do I do this efficiently? I would like to use a while loop or for loop except I am unable to index the string. I converted the string to constant char so I could index it but now I am unable to compare the value at a point in the string to the expression " " in my if statement. What should I do?

Compare the current symbol to ' '.

Related

How do i "subtract" a character from a string in c++ [duplicate]

This question already has answers here:
Remove last character from C++ string
(12 answers)
Closed 8 months ago.
Lets say I have a string variable with the value "bananas" in it. I want to subtract the last letter so the string becomes "banana". I am quite a newbie, so I dont even know how to tackle this.
Just use the pop_back() function.
Try this code, it 'subtracts' the last character:
std::string str = "bananas";
str.pop_back();

Inserting a newline near the end of a std::string [duplicate]

This question already has answers here:
C++: insert char to a string
(4 answers)
Closed 3 years ago.
I'm struggling with trying to implement a C++ code solution that will allow me to insert a newline (i.e. a string literal '\n') towards the end of a std::string, and not at the very end as most implementations show.
For example, I want to insert a '\n' just -1 characters before the very end itself. So if the string was 100 characters long (poor analogy I know), then I'd like to insert the string literal at the 99th character in a clean, easily readable manner.
Thanks!
Here's one way:
std::string test{"abcdef"};
if (!test.empty())
test.insert(test.length() - 1, "\n");
and here's one based on iterators:
if (!test.empty())
test.insert(std::prev(test.end()), '\n');

In C++, how do I take a string formatted like "######" and store each character as an integer? [duplicate]

This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed 7 years ago.
My function takes a large number as input, in the form of a string. I need to store each number in an integer array, but have been unable to do so. When I do a for loop and make array[i] = string[i], it saves the number as its ascii value. I've been trying to convert this number from its ascii value to an integer, but I cant get atoi to work. Any suggestions?
Since the numerals '0'-'9' are required to be encoded consecutivly in base character set, the numeric value of a numeral character c is simply c - '0'.

Allocating words from a string separated by spaces to a variable? C++ [qt] [duplicate]

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 9 years ago.
I just wanted to know what the technical term for allocating words from a string separated by spaces to a variable is called so I can look up a tutorial for it. Any terms, links, or explanations would be greatly appreciated.
You can do it like:
QString lineText = "some sample words";
QStringList tokens= lineText.split(" ",QString::SkipEmptyParts);
You can do whatever you like with the words in the tokens list.

C++: Comparing two strings [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
comparing two strings with comma seperated values
I am working in C++, where I have two strings:
string str1 = "1,4,8,",
str2 = "4,1,8,";
Both strings contains comma separated values. Now I just want to check whether all the elements in str1 also exist in str2, regardless of their position. Is there any direct way to check this? Do I need to write custom code for this?
As far as C++ is concerned, those strings are just sequences of characters. If you apply meaning to those characters (such as "comma separated values"), then you'll have to write some code to extract the data and deal with it.
I would do something like:
split the string on ','
convert each sequence of digits into an integer (skipping over empty elements)
insert those integers into a set (one for each input string)
compare the sets
It's up to you to determine what kind of integer to use.
Yes, you need to write custom code, although not a lot of it. Once you figure out the algorithm you can post here if you have further questions on how to implement each part.