Increment Operator Question

Write your question here.
How do you read this statement words[*stream_begin++]++;?
(1) Why is there an increment operator (++) after *stream begin and after [*stream begin++]?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//	Ex10_13.cpp
//	A simple word collocation

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <iterator>

int main()
{
	std::map<std::string, int> words;  //Map to store words and word counts
	std::cout << "Enter some text, press Enter followed by Ctrl-Z"
		" then Enter to end:\n"
		<< std::endl;

	std::istream_iterator<std::string> stream_begin{ std::cin }; //Stream iterator
	std::istream_iterator<std::string> stream_end;	//End stream iterator

	while (stream_begin != stream_end)  //Iterate over words in the stream
	     words[*stream_begin++]++; //Increment and store a word count

	//	Output the words and their counts
	std::cout << "Here are the word counts for the text you entered:" << std::endl;
	const int wordsPerLine{ 4 };
	int wordCount{};
	std::cout << std::setiosflags(std::ios::left);	//Output left-justified
	for (const auto& word : words)
	{
		std::cout << std::setw(12) << word.first << " " << std::setw(5) << word.second;
		if (++wordCount % wordsPerLine == 0)  std::cout << std::endl;
	}
	std::cout << std::endl;

	return 0;
} 
Operator precedence in c++ is hierarchical and tricky in statements like that.
http://en.cppreference.com/w/cpp/language/operator_precedence
unary * and ++ are both is in precedence level 3, so they are evaluated right-to-left. Since the * comes first the pointer stream_begin is dereferenced first, after that the value pointed by the pointer is incremented because it was dereferenced. Essentially *stream_begin++ by itself is able to iterate through a list.
Hopefully the second ++ makes more sense now. :)

I overlooked the details of prefix and postfix while being distracted by tv earlier. See dhayden's post below.
Last edited on
Thanks for your response.

According to your link of C++ Operator Preference, the post increment operator(++) is level 2 and the derefence operator (*) is level 3. So, your logic does not follow.

However, you do bring up a point that *stream_begin++ is used to increment *stream_begin to interate through the while loop. So, it looks like *stream_begin accesses a word and stores it in the map container, and the expression *stream_begin++ increments the iterator after accessing the word to cycle through the while loop.

The whole expression words[*stream_begin++]++ increments the count for the word.

I believe that is how the expression works.

Thank you.

suffix ++ is actually at precedence level 2. So is []. * is at level 3. Level 2 associates left to right. In other words, the expression evaluates like this:
(words [ * (stream_begin++) ]) ++
So the value of the expression is the same as words[*stream_begin]. It has the side effect of incrementing stream_begin and words[*stream_begin].
dhayden,

Thank you very much for your comment.

I added the parenthesis to the expression in the program and ran it. By golly, it worked. However, I changed the expression because I had a difficult time comprehending it to as follows: (words[ *(stream_begin) ++ ]) ++ . This also worked, but it gives me a better understanding of the operation.

First, as indicated by the parenthesized expression, the stream iterator reads words from cin. Next, the dereferencing occurs to store the value into the map container. After storing the value, stream_begin is incremented (postfix) in order to cycle through the while loop. Last, the whole expression (words[*(stream_begin) ++ ]) ++ will increment the word count and be stored.
Topic archived. No new replies allowed.