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 dynamically allocate arrays in C++
- How to convert string to char array in C++?
- How can I convert a std::string to int?
- Easiest way to convert int to string in C++
- 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?
- What is `CString`?
- Initializing an array of objects
- How do I print out the contents of a vector?
- Passing an array by reference
- Return array in a function
- How do I include the string header?
- check if a std::vector contains a certain object?
- Passing an array by reference
- How to navigate through a vector using iterators? (C++)
- Differences between C++ string == and compare()?
- C++ for each, pulling from vector elements
- How do I erase an element from std::vector<> by index?
- How to print elements in a vector c++
- What is the array form of ‘delete’?
- How do I convert a double into a string in C++?
- Split a string using C++11
- Use new operator to initialise an array
- Is it more efficient to copy a vector by reserving and copying, or by creating and swapping? [duplicate]
- How do I iterate over the words of a string?
- How do I declare a 2d array in C++ using new?
- How to convert an instance of std::string to lower case
- 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?
- Are vectors passed to functions by value or by reference in C++
- Returning an empty string : efficient way in c++
- Replace part of a string with another string
- C++ string to double conversion
- Debug assertion failed. C++ vector subscript out of range
- No matching member function for call to ‘push_back’ error
- Pass a vector by reference C++
- What is use of c_str function In c++
- lvalue required as left operand of assignment – Array
- How to reverse an std::string? [duplicate]
- Why there is no pop_front method in C++ std::vector?
- Initializing a two dimensional std::vector
- C++ – No matching member function for call to ‘push_back’
- C++ Parsing input string to variables
- Debug assertion failed. C++ vector subscript out of range
- Vector is not a Template?
- Displaying a vector of strings in C++
- std::string to char*
- ‘vector’ in namespace ‘std’ does not have a template type
- Is there a function to copy an array in C/C++?
- 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++?
- Why use string::iterator rather than index?
- Reverse Contents in Array
- C++ error: “Array must be initialized with a brace enclosed initializer”
- Why am I getting string does not name a type Error?
- warning: ISO C++ forbids variable length array
- c++ array – expression must have a constant value
- C++ error: Undefined symbols for architecture x86_64
- Vector of structs initialization
- C++ deprecated conversion from string constant to ‘char*’
- Passing a 2D array to a C++ function
- identifier “string” undefined?
- Press Enter to Continue
- vector
::size_type in C++ - Parsing a comma-delimited std::string
- How can I convert const char* to string and then back to char*?
- std::string formatting like sprintf
- Sorting Characters Of A C++ String
- basic_string::_M_construct null not valid after constructing subvector of strings
- How to copy a string of std::string type in C++?
- How to return a string from a C++ function?
- Why use a new call with a C++ ‘vector’?
- std::out_of_range error?
- Static array vs. dynamic array in C++
- How to create a dynamically-allocated array of const objects, but have values assigned to them?
- What is the difference between isdigit() and isnumber()?
- Convert char* to string C++
- Why doesn’t std::vector::push_front() exist?
- C++ delete vector, objects, free memory
- How to iterate over a vector?
- How to access the contents of a vector from a pointer to the vector in C++?
- printf with std::string?
- Convert a String In C++ To Upper Case