Please tell me what is the error?
vector<char> str; int a; while(cin >> a) str.push_back(a); for(vector<char>::const_iterator i = str.begin(); i != str.end(); ++i) cout << *i; According to my idea, the entered string should be displayed.
Please tell me what is the error?
vector<char> str; int a; while(cin >> a) str.push_back(a); for(vector<char>::const_iterator i = str.begin(); i != str.end(); ++i) cout << *i; According to my idea, the entered string should be displayed.
You do not enter characters, as you think, but numbers. So if you type something different from an integer, then, respectively ...
vector<char> str; char a; while(cin >> a) str.push_back(a); for(vector<char>::const_iterator i = str.begin(); i != str.end(); ++i) cout << *i; This is how it will work, only the input you need to complete by pressing Ctrl-Z on Windows (on Linux Ctrl-D, it seems ... I don’t remember exactly) - since you check the state of the cin stream.
But why don't you work with string strings?
a and exit the loop when detecting '\n' . - AnTTo get the expected result, you have to enter by a separate digit, such as 1 2 3 4 5 6 7 8 9 0, and convert these integer values into character codes of the numbers, as shown below in the demo program
#include <iostream> #include <vector> int main() { std::vector<char> str; int a; while ( std::cin >> a ) str.push_back( a + '0' ); for ( char c : str ) std::cout << c; std::cout << std::endl; return 0; } If you enter not a single digit, e some number, the result will no longer be predictable.
There is a standard function std::to_string , which translates a number into an object of type std::string .
For example, you could write
#include <iostream> #include <vector> #include <string> int main() { std::vector<char> str; int x; while (std::cin >> x) { std::string tmp = std::to_string(x); if (!str.empty()) str.push_back(' '); str.insert(str.end(), tmp.begin(), tmp.end()); } for (char c : str) std::cout << c; std::cout << std::endl; } int a;
char a; ... str.begin(); i != str.end(); ++i
... str.cbegin(); i != str.cend(); ++i // ^__________________^--------------- константный begin end overloads that return constant iterators. In this example, they are called - yrHeTaTeJlbSource: https://ru.stackoverflow.com/questions/598932/
All Articles
vector< vector<char> > text;objectvector< vector<char> > text;Can we, for example, considertext[i].size? - Freddy