Link to home
Start Free TrialLog in
Avatar of Nusrat Nuriyev
Nusrat NuriyevFlag for Azerbaijan

asked on

C++ STL: three question about vectors

#include <iostream>
#include <vector>
using namespace std;

int main()
{

double array[5] = {3.45, 67, 10, 0.67, 8.99};
vector<double> vec;
vec.assign(array, array + 5);
vector<double>::iterator it;

*it = vec.front();
cout << *it << " ";
*it = vec.back();

// 1.0 Here I get Access denied.

it = &vec.front();
// 2 Here I got compiler error

it = vec.begin();
vec.insert(it, 3, 90);
cout << *it;
// 3. Here I can't use insert function.
    return 0;
}

Open in new window

Avatar of phoffric
phoffric

In line 13, I would like you to go into your debugger and check out the value of it.
For line 19, please review the return type of std::vector::front
http://www.cplusplus.com/reference/vector/vector/front/

and compare that type with the type of it.
For line 23...
Well, many professionals on their job, when they get multiple problems in one compilation or build, will fix one or two issues and then rebuild. For this line, I would work on the first two problems, and from what you have learned, maybe you will be able to easily fix this problem.
*it = vec.front();
the statement is invalid. unlike to std::vector::begin std::vector::front doesn't return an iterator but a direct reference to the first vector element.

so, the right side of the assignment is double& .

the left operand of the assignment however is invalid as it tries to dereference an uninitialized iterator. actually the compiler should bring at least a warning. in any case the behavior at runtime cannot be predicted as it depends on the (arbitrary) value the variable it has.

1.0 Here I get Access denied.
as told, dereferencing an iterator variable which is pointing to nowhere will show odd behavior. one of that might be 'access denied' which i never experienced.

// 2 Here I got compiler error
as told std::vector::front returns a reference to double which cannot assigned to an iterator (what is a pointer to a vector element).

// Here I can't use insert function.
if you insert at begin using an iterator it that pointed to the first element, the 3 elements were inserted at front o fthe vector. because of that 'moving' operation the it points to a memory location which most likely was freed by the insert operation. so, the statement 'cout << *it;' tries to reference a variable it which is invalid.

note, an iterator is similar to a pointer. if you don't make sure that an iterator variable points to a valid container element, you don't can expect that the iterator variable can be used correctly.

Sara
Avatar of Nusrat Nuriyev

ASKER

Phoffric,  Yes, this is a homework. The homework is for my students.  I'm a teacher of C programming language.
These questions requries a minute or above to answer for C++ expert.
However, it will be better if I spend time to investigate that by myself.

First of all, in line 13 , there is a mistake .
it's like
int n = 5;
int *pi;
*pi = n;

Open in new window

Iterator is just an address, so it must be pointed to some address.
instead of this
vector<double>::iterator it;

Open in new window

will be
vector<double>::iterator it = vec.begin();

Open in new window

sarabande,
about 2-nd

For iterator manipulation, there are two functions of vector which come in handy: the front() function which returns an iterator to the first element in the vector, and the back() which returns an iterator to the last element in the vector.
http://www.codeproject.com/Articles/20930/The-complete-guide-to-STL-Part-Vector

Has it been changed since 2007?
Line 13 - you are correct.

>> // 1.0 Here I get Access denied.
Well, what was the value of it on your machine?
Could it have been 0, which is not a good address?
There is potentially a 4th problem, which if you find to be a problem could be interesting to discuss in another question.
SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In my earlier post I wrote:
For line 19, please review the return type of std::vector::front
http://www.cplusplus.com/reference/vector/vector/front/
 and compare that type with the type of it.
From that link:

           reference front();
const_reference front() const;

Access first element

Returns a reference to the first element in the vector.

 Unlike member vector::begin, which returns an iterator to this same element, this function returns a direct reference.
Then &vec.front() is the address of that first reference (i.e., the address of a double). So &vec.front() type is double*, which is not an iterator type. Iterators can be used to look like pointers, but they are not pointers.
For the 4th (unstated) problem, read this link
http://www.cplusplus.com/reference/vector/vector/insert/
and search on the string valid and read what it says in the source code example as well as the Iterator validity section.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial