Question: I just asked someone with the same question, but now I am more confused. The original question was: Hi Dear Tutors: in C++, say if
I just asked someone with the same question, but now I am more confused.
The original question was:
Hi Dear Tutors: in C++, say if have the following code: map > set; // adding elements to set, and set int n = set.size() Can I make a pointer to set.size() ? And what about a layer further... lets say, a pointer to .. set[2].size() ?? How can it be done?Yes, it does have those codes You can't make a pointer to set.size(). If you want to make a pointer to the size of set[2], you can do int *ptr = &set[2].size0; However, this pointer will be invalidated it you insert/erase elements from the vector. Explanation: The first code creates a map called set, with keys that are integers, and values that are vectors of integers This map can be used to store sets of integers, with each set being identified by a unique key. The second code adds elements to the set map, by creating new vectors of integers and adding them to the map using the integer keys. This allows the sets to be stored in the map and accessed using the keys. The third code retrieves the size of the set map, which is the number of sets that have been stored in the map. This can be used to determine how many sets are in the map, and to iterate over them. You can't make a pointer to set.size(). This is because set.sizeO is a function, and you can't make a pointer to a function. It you want to make a pointer to the size of set[2], you can do int *ptr = &set[2].size0; However, this pointer will be invalidated it you insert/erase elements from the vector. This is because when you insert/erase elements from a vector, the memory location of the vector can change, which would make the pointer invalid
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
Students Have Also Explored These Related Programming Questions!