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.

  • and if, let's say, we have a vector< vector<char> > text; object vector< vector<char> > text; Can we, for example, consider text[i].size ? - Freddy

3 answers 3

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?

  • Vector is connected, the completion of Ctrl-D on Linux does not help, the string is not displayed. I think maybe the problem is in the qt itself - Freddy
  • Everything, the question is settled. I sleep on the go. Thank! - Freddy
  • But such another question, is it possible to avoid a key combination to check the flow of cin, using enter at the end of the input? - Freddy
  • @Freddy: Well, do it yourself with pens and check the entered value a and exit the loop when detecting '\n' . - AnT

To 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 // ^__________________^--------------- константный 
    • There are begin end overloads that return constant iterators. In this example, they are called - yrHeTaTeJlb
    • @yrHeTaTeJlb Not a constant iterator is called. But it can be compared with a constant iterator. - Vlad from Moscow
    • @yrHeTaTeJlb, there are almost no overloads based on the return value. - Qwertiy
    • @Qwertiy, they are never. Almost none. There is no overload in the link response. - ixSci
    • @ixSci, I know, just wrote like that :) In general, in this case the same thing - the return type is converted to another. - Qwertiy