Set Insert Iterator?

1
2
fin >> k;    
gr.insert(--k); 

what does this code mean?
What's fin? Since some value is being extracted from it and inserted into k it would be some form of a stream. file stream? keyboard stream? Can't tell

What data type is k? By the decrement prefix operator a GUESS would be some form of an integer type.

Or could be a custom type with a prefix decrement operator.

What type of container is gr? Looks like an STL container with an insert member function. Maybe a std::vector, std::deque or std::list?

1
2
3
fin >> k;    //input k
set<int> gr;
gr.insert(--k);
Well as far as the insert is concerned, you've done
gr.insert(k-1);

The --k also means k itself is changed.
Input k from where? What is fin? An opened file stream? An alias for std::cin? What?

In a roundabout way you tell us what k is, an int because you have instantiated a std::set to contain ints.

std::set::insert
http://www.cplusplus.com/reference/set/set/insert/

Your insert function theoretically inserts a new set element with a decremented value of k. If the value doesn't already exist in gr.

Your compiler may or may not decrement k before it inserts the value into gr. It may optimize the decrement to be performed AFTER the std::set::insert.

so I insert k-1, and k itself is also decremented by 1?
Last edited on
--k is NOT the same as k - 1. The decrement prefix operator changes the value of k. If k = 3, then --k sets k = 2.

k - 1 doesn't change the value contained in k. After the insert k still = 3.
so K is changed to k-1 and inserted, and then k now equals k-1 right.
Why did you remove the code you posted? It had the context that answered most of the questions I kept asking.

Posting a 2/3 line snippet has no context to answer you intelligently without asking a lot of very simple questions.
1
2
fin >> k;
gr.insert( --k );

is same as
1
2
3
fin >> k;
--k;
gr.insert( k );


--k; is same as k = k - 1;
Topic archived. No new replies allowed.