Understanding a C ++ Program [Bjarne Stroustrup book]

I need your precious help for a little question! I am reading a Bjarne Stroustrup book and I found this example:

int main()
{
   string previous = " ";
   string current;

   while (cin >> current) {                                    
      if(previous == current)
        cout << "repeated word: " << current << '\n';
      previous = current;
   }                                                            
   return 0;
}

My question is: what does the line previous = "" do; make?

Initializes the previous character space (for example, by pressing the spacebar). But I thought that in C ++ he does not read it, something about the compiler, skipping spaces. Why initialize it before that?

I tried to write like this: string previous; , and the program still works correctly ... right? What is differnece? Please enlighten me x)

+4
source share
4

, - , std::cin. , previous , (, ), , current.

previous , istream::operator>> , , , std::cin . std::cin (, getline()), .

.

+3

, , ++ . ++

std::string the_string = something;

,

std::string      the_string=something          ;

, , charcters .

std::string foo = " ";

,

std::string foo = "    ";

4 .

+4
string previous = " ";

'previous'.

"", , " ".

+1

:)

string previous;

, operator >> , .

, current , , false.

+1
source

Source: https://habr.com/ru/post/1613245/


All Articles