• A permanent pointer to an element in a vector
    6 replies, posted
I need a way to have a pointer permanently point to an element in a std::vector. Using a pointer to an element works fine, until another element is added, as demonstrated by the following code: [cpp]#include <iostream> #include <vector> int main( ) { std::vector<int> numbers; numbers.push_back( 1337 ); std::vector<int>::pointer pfn = &numbers[0]; std::cout << "pfn -> " << pfn << " : " << *pfn << std::endl; numbers.push_back( 1234 ); std::cout << "pfn -> " << pfn << " : " << *pfn << std::endl; std::cin.get( ); return 0; }[/cpp] The output is: [code]pfn -> 003469C8 : 1337 pfn -> 003469C8 : -17891602[/code] I understand that when working with arrays it's not possible to keep an element at the same memory address when resizing, so how should I solve this? I could store an integer with the index at which the element resides, but I really need a function to return a pointer, so the element can be modified. The problem is that other elements will be created, but elements other than the last one still need to be modified.
Three options: 1) don't do that 2) use a vector of integer pointers, that way you can pass stuff the actual heap-allocated memory address of the int, and the vector will only mess with the address of the pointers 3) use an std::list
Awesome. Now, is there any shorter way to do the code between the equal signs? [cpp]#include <iostream> #include <list> int main( ) { std::list<int> numbers; numbers.push_back( 1 ); numbers.push_back( 2 ); numbers.push_back( 3 ); // ============================================================================= std::list<int>::iterator it = numbers.begin( ); it++; int* pfn = &*it; // ============================================================================= std::cin.get( ); return 0; }[/cpp] The following didn't work by the way: [cpp]int* pfn = (int*)it;[/cpp] [code]error C2440: 'type cast' : cannot convert from 'std::list<_Ty>::_Iterator<_Secure_validation>' to 'int *'[/code]
You could store the index of the number instead of using a pointer.
[QUOTE=Overv;16119768]Awesome. Now, is there any shorter way to do the code between the equal signs?[/QUOTE] How about [cpp] int* pfn = &*(numbers.begin() + 1); [/cpp] I have no idea if it will work.
Hmm, it seems only this works: [cpp]std::list<int>::iterator it = numbers.begin( ); it++;[/cpp] The following causes an error: [cpp]std::list<int>::iterator it = numbers.begin( ); it += 1;[/cpp] [code]error C2676: binary '+=' : 'std::list<_Ty>::_Iterator<_Secure_validation>' does not define this operator or a conversion to a type acceptable to the predefined operator[/code] Looks like I'm gonna need a function.
[QUOTE=Overv;16121113]Hmm, it seems only this works: [cpp]std::list<int>::iterator it = numbers.begin( ); it++;[/cpp] The following causes an error: [cpp]std::list<int>::iterator it = numbers.begin( ); it += 1;[/cpp] [code]error C2676: binary '+=' : 'std::list<_Ty>::_Iterator<_Secure_validation>' does not define this operator or a conversion to a type acceptable to the predefined operator[/code] Looks like I'm gonna need a function.[/QUOTE] it += 1; doesn't work but it = it + 1; does.
Sorry, you need to Log In to post a reply to this thread.