Returning a pointer to a vector element in C ++

I have a vector myObjects in a global scope. I have a method that uses std::vector<myObject>::const_iterator to move a vector and do some comparisons to find a specific element. As soon as I find the required element, I want it to be able to return a pointer to it (a vector exists in the global area).

If I return &iterator , will I return the address of the iterator or the address pointed to by the iterator?

I need to return const_iterator back to myObject and then return the address of this file

+49
c ++ iterator pointers containers
Mar 13 '09 at 8:54
source share
9 answers

Returns the address of the object that the iterator points to:

 &(*iterator) 

Edit: To fix some confusion:

 vector <int> vec; // a global vector of ints void f() { vec.push_back( 1 ); // add to the global vector vector <int>::iterator it = vec.begin(); * it = 2; // change what was 1 to 2 int * p = &(*it); // get pointer to first element * p = 3; // change what was 2 to 3 } 

No need for pointer vectors or dynamic allocation.

+72
Mar 13 '09 at 9:01
source share

The returned iterator will return the address of the iterator. If you want to return the method of accessing the element, return the iterator itself.

Beware that the vector is not global in order to return an iterator / pointer, but these operations in the vector can invalidate the iterator. For example, adding elements to a vector can move vector elements to another position if the new size () is larger than the reserved memory. Removing the element before this element from the vector will cause the iterator to refer to another element.

In both cases, depending on the implementation of the STL, it is difficult to debug using random errors that happen so often.

EDIT after comment: "yes, I didn’t want to return the iterator a) because its const, and b) is it probably just a local, temporary iterator? - Krakkos'

Iterators are no more or less local or temporary than any other variable, and they can be copied. You can return it, and the compiler will make a copy for you, as it will with a pointer.

Now with a constant. If the caller wants to make changes through the returned element (be it a pointer or an iterator), you must use a non-constant iterator. (Just remove the 'const_' from the iterator definition).

+12
Mar 13 '09 at 9:03
source share

Returning iterators is not recommended. Iterators become invalid when changes in the vector occur (inversion \ deletion). In addition, an iterator is a local object created on the stack and, therefore, the returning address is not safe at all. I suggest you work with myObject, not vector iterators.

EDIT: If the object is light, then it is better to return the object. OtherIwise returns pointers to myObject stored in the vector.

+3
Mar 13 '09 at 9:02
source share

As long as your vector remains in the global scope, you can return:

 &(*iterator) 

I warn you that this is generally quite dangerous. If your vector is ever taken out of the global scope and destroyed, any pointers to myObject become invalid. If you write these functions as part of a larger project, returning a non-const pointer may cause someone to delete the return value. This will have undefined and catastrophic consequences for the application.

I would rewrite this as:

 myObject myFunction(const vector<myObject>& objects) { // find the object in question and return a copy return *iterator; } 

If you need to modify the returned myObject, save your values ​​as pointers and allocate them on the heap:

 myObject* myFunction(const vector<myObject*>& objects) { return *iterator; } 

Thus, you have control over when they are destroyed.

Something like this will break your application:

 g_vector<tmpClass> myVector; tmpClass t; ti = 30; myVector.push_back(t); // my function returns a pointer to a value in myVector std::auto_ptr<tmpClass> t2(myFunction()); 
+3
Mar 13 '09 at 12:34
source share

You can use the data function for the vector:

Returns a pointer to the first element in a vector.

If you do not want the pointer to the first element, but by index, you can try, for example:

 //the index to the element that you want to receive its pointer: int i = n; //(n is whatever integer you want) std::vector<myObject> vec; myObject* ptr_to_first = vec.data(); //or std::vector<myObject>* vec; myObject* ptr_to_first = vec->data(); //then myObject element = ptr_to_first[i]; //element at index i myObject* ptr_to_element = &element; 
+1
Sep 05 '14 at 19:41
source share

Let's say you have the following:

 std::vector<myObject>::const_iterator first = vObj.begin(); 

Then the first object in the vector: *first . To get the address, use: &(*first) .

However, in accordance with the STL design, I would suggest instead returning an iterator if you plan on passing it later to STL algorithms.

0
Mar 13 '09 at 9:02
source share

You save copies of myObject in a vector. Therefore, I believe that copying an instance of myObject is not an expensive operation. Then I think the safest one will return a copy of myObject from your function.

0
Mar 13 '09 at 10:21
source share

Refer to the dirkgently and anon answers, you can call the front function instead of the begin function, so you should not write * , but only & .

Code example:

 vector<myObject> vec; //You have a vector of your objects myObject first = vec.front(); //returns reference, not iterator, to the first object in the vector so you had only to write the data type in the generic of your vector, ie myObject, and not all the iterator stuff and the vector again and :: of course myObject* pointer_to_first_object = &first; //* between & and first is not there anymore, first is already the first object, not iterator to it. 
0
Sep 05 '14 at 20:29
source share

I am not sure that the address of the thing specified by the iterator needs to be returned. All you need is the pointer itself. You will see that the STL iterator class itself uses the use of _Ptr for this purpose. So just do:

 return iterator._Ptr; 
0
Nov 29 '14 at 17:12
source share



All Articles