It is possible to turn the string into a stream by using the std::stringstream
class (its constructor takes a string as parameter). Once it’s built, you can use the >>
operator on it (like on regular file based streams), which will extract, or tokenize word from it:
#include <iostream> #include <sstream> using namespace std; int main(){ string line = "test one two three."; string arr[4]; int i = 0; stringstream ssin(line); while (ssin.good() && i < 4){ ssin >> arr[i]; ++i; } for(i = 0; i < 4; i++){ cout << arr[i] << endl; } }
Related Posts:
- How to convert a char array to a string?
- Converting a vector
to string - How to convert vector to array
- Correct way to work with vector of arrays
- Dynamically allocated string array, then change it’s value?
- How to implement 2D vector array?
- Why the switch statement cannot be applied on strings?
- How to dynamically allocate arrays in C++
- How to convert string to char array in C++?
- How can I convert a std::string to int?
- What is the easiest way to initialize a std::vector with hardcoded elements?
- Vector of Vectors to create matrix
- std::string to char*
- How do I find the length of an array?
- How to convert string to char array in C++?
- C++ — expected primary-expression before ‘ ‘
- C++ string to double conversion
- Conversion from string to char – c++
- How to find out if an item is present in a std::vector?
- Initializing an array of objects
- How do I print out the contents of a vector?
- Passing an array by reference
- C++ for each, pulling from vector elements
- How do I erase an element from std::vector<> by index?
- What is the array form of ‘delete’?
- How do I convert a double into a string in C++?
- Use new operator to initialise an array
- Check if a string contains a string in C++
- How do I reverse a C++ vector?
- How do I iterate over the words of a string?
- Replace part of a string with another string
- What is use of c_str function In c++
- How to reverse an std::string? [duplicate]
- Initializing a two dimensional std::vector
- C++ – No matching member function for call to ‘push_back’
- C++ Parsing input string to variables
- std::string to char*
- How to convert a std::string to const char* or char*
- Cleanest way to copy a constant size array in c++11
- How do I tokenize a string in C++?
- Reverse Contents in Array
- Press Enter to Continue
- vector
::size_type in C++ - Parsing a comma-delimited std::string
- How to copy a string of std::string type in C++?
- Why use a new call with a C++ ‘vector’?
- Static array vs. dynamic array in C++
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- Why doesn’t std::vector::push_front() exist?
- Convert a String In C++ To Upper Case
- Insert object at index of vector c++
- Right way to split an std::string into a vector
- Read whole ASCII file into C++ std::string
- Fill array with random numbers within a specified range (C++)
- Remove an array element and shift the remaining ones
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- C++: std does not have member “string”
- Right way to split an std::string into a vector
- string subscript out of range error
- No operator << matches these operands
- Returning an empty string : efficient way in c++
- How to find the size of an int[]?
- What’s the syntax for declaring an array of function pointers without using a separate typedef?
- Remove spaces from std::string in C++
- Delete 2D array C++
- Remove spaces from std::string in C++
- Best way to extract a subvector from a vector?
- How do you initialise a dynamic array in C++?
- C++ strings and pointers
- How to Convert a C++ String to Uppercase
- std::wstring VS std::string
- Reading from .txt file into two dimensional array in c++
- C++ Simple hangman game
- Remove First and Last Character C++
- C++ Remove punctuation from String
- C++ string to enum
- C++ Convert string (or char*) to wstring (or wchar_t*)
- C++ – Assigning null to a std::string
- Resizing dynamic array in c++
- Reading string by char till end of line C/C++
- C++ Initializing a Global Array
- How can I assign an array from an initializer list?
- Fastest way to Convert String to Binary?
- Multidimensional Vectors in C++
- Reading data from file into an array
- Is string::compare reliable to determine alphabetical order?
- Trying to use int in getline
- C++ pass an array by reference
- “vector” was not declared in this scope
- creating an array of structs in c++
- How to do std::string indexof in C++ that returns index of matching string?
- too many initializers for ‘int [0]’ c++
- variable or field declared void
- Vector declaration “expected parameter declarator”
- Using cin for char array
- convert string to size_t
- Different ways to deallocate an array – c++
- Why can’t I make a vector of references?
- Why are there two different getline() functions (if indeed there are)?
- Why does my program give “NULL used in arithmetic”