Solved

C++ STL: three question about vectors

Posted on 2014-12-22
13
108 Views
Last Modified: 2015-01-14
#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

0
Comment
Question by:Nusrat Nuriyev
  • 7
  • 3
  • 2
13 Comments
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
In line 13, I would like you to go into your debugger and check out the value of it.
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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.
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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.
0
 
LVL 32

Expert Comment

by:sarabande
Comment Utility
*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
0
 

Author Comment

by:Nusrat Nuriyev
Comment Utility
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

0
 

Author Comment

by:Nusrat Nuriyev
Comment Utility
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?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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?
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
There is potentially a 4th problem, which if you find to be a problem could be interesting to discuss in another question.
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 500 total points
Comment Utility
Has it been changed since 2007?
no. the statement in the article is wrong. in the following code sample they have

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

Open in new window


what makes clear that front and back are returning a reference to the first respectively the last element. the iterator of stl-part takes the address of the return value. former implementations of stl were using simple pointers as iterators like it is for c-type arrays. newer implementations use a class type derived from std::iterator.

see http://www.cplusplus.com/reference/vector/vector/front/for more information.

Sara
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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.
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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.
0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
Iterators can be used to look like pointers, but they are not pointers.

iterators also may be pointers. actually in the early times of stl, vector<T>::iterator was indeed a simple pointer to T.

even nowadays you may use pointers for iterators if the container is a c array:

double dblarr[4] = { 0.1, 0.3, 1.2, -1.5 };

// use pointer to begin and pointer to end of dblarr to initialize a vector
std::vector<double> dblvec(&dblarr[0], &dblarr[4]);

Open in new window


the snippet show the use of a constructor of std::vector that has the following signature:

template<class _Iter>
		vector(_Iter _First, _Iter _Last,
			typename enable_if<_Is_iterator<_Iter>::value,
				void>:: type ** = 0) 

Open in new window


a pointer can be used as iterator if one of the following applies:

(1) the element the pointer is pointing to, is an array  element of the requested type
      (what is also the case for pointers to the internal array of std::string and std::vector)
(2) the iterator of the container is defined by a typedef of a pointer to template type
(3) the template iterator class used has a constructor that takes a pointer to template type as argument

(2) and (3) are dependent on the platform-specific stl implementation.

Sara
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now