Using an iterator in a for loop

Hey guys, just running into a small problem, and not sure if what I am attempting is possible, but I don't see why it wouldn't be. I am just delving into STL components and writing simple programs to try them out.

I have a program that needs to regularly print vectors, so I made a function to do so. It worked fine with a regular for loop (int i = o, etc), but I wanted to try it with an iterator.

The function:

1
2
3
4
5
6
7
8
9
10
11
12
13
void printVector(const vector <int> *inVector, const string inName)
{
	vector<int>::iterator it;

	cout << inName << " is size " << inVector->size() << " with a " << inVector->capacity() << " element capacity:\n";

	for (it = inVector->begin(); it != inVector->end(); it++)
	{
		cout << *it << " ";
	}

	cout << "\n\n";
}


The call:
printVector(&vecOne, "vecOne");

The error:
Error	1	error C2679: binary '=' : no operator found which takes a right-hand operand of type 
'std::_Vector_const_iterator<_Ty,_Alloc>' (or there is no acceptable conversion)	109


I am just trying to set 'it' to the beginning of the vector, then move through to the end, printing each element. My error messages come from both '=' and '!=' operators within the for parameters. I feel like it should work, but my syntax is off since it is operating from a pointer to the vector, rather than the vector itself.

I am venturing outside of what I've been taught over school break, so I'm a bit over my head in a few instances. I am writing in Visual Studio 2008, if that matters. Any help on the problem or commentary on other aspects of the code is appreciated, thank you.
You are passing in a const vector pointer so you need to use a const iterator:

 
vector<int>::const_iterator it;
Also its good practice to always favour passing a const reference to any objects passed in as arguments to a function.

 
void printVector(const vector<int>& inVector, const string& inName)


There is no point in copying the object if it is not going to be modified.

Last edited on
Have you seen this technique? It is better to not write your own function when you can use existing template solutions.
http://cplusplus.com/reference/std/iterator/ostream_iterator/

It works for user defined types as well.
http://cplusplus.com/forum/general/20346/
Thank you. I rewrote the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
void printVector(const vector<int>& inVector, const string& inName)
{
	vector<int>::const_iterator it;

	cout << inName << " is size " << inVector.size() << " with a " << inVector.capacity() << " element capacity:\n";

	for (it = inVector.begin(); it != inVector.end(); it++)
	{
		cout << *it << " ";
	}

	cout << "\n\n";
}


and the call:

printVector(vecOne, "vecOne");

It works fine now. My question is about your saying "There is no point in copying the object if it is not going to be modified." Were you referring to the vector, string, or both?

Not wanting to make a copy of the vector was the reason I used a pointer to pass it in the first place... Doesn't that just send an address to the first element, similar to using a reference like you had me change it to? This makes me think you were referring to the string.

I thought that passing pointers and references was a way to reference a variable in a different scope without having to create a copy. Since in my main() there is no string variable holding the name (it is just hard-coded into the call) to be passed, I didn't know that I could use a reference for it. Unless I am way off, what is that string& referencing?

Thank you for the help. I understand the syntactical differences between ref's and pointers, but apparently not the deeper implementation and "when to use which". Just another topic for my summer reading now.

Kempofighter, thank you for those links, I read/bookmarked them both. I just don't know if I grasp how it works.

Sample code:
1
2
3
4
5
6
7
8
int main () {
  vector<int> myvector;
  for (int i=1; i<10; ++i) myvector.push_back(i*10);

  ostream_iterator<int> out_it (cout,", ");
  copy ( myvector.begin(), myvector.end(), out_it );
  return 0;
}


I read this (line 5 and 6) as "Create an ostream_iterator for type int named out_it. Its purpose is to print the contents of the element, a comma and a space." So why use the copy algorithm? Is this now saying "Copy from myvector's begin to end into out_it", which will then cout and add punctuation?

I have never used ostream_iterators or copy, I'll put them on the list.

Thanks again to the both of you.

Soaps wrote:
It works fine now. My question is about your saying "There is no point in copying the object if it is not going to be modified." Were you referring to the vector, string, or both?


I was talking about object parameters in general. I noticed you passing the string by value and that made me think of it. Afterwards I realised it also applies to your vector.

Pointers do achieve the same thing with regards to avoiding a copy. However references should really be used in preference to pointers as they are guaranteed to have been initialised.

Soaps wrote:
Not wanting to make a copy of the vector was the reason I used a pointer to pass it in the first place... Doesn't that just send an address to the first element, similar to using a reference like you had me change it to? This makes me think you were referring to the string.


Sending a pointer to a vector is not the address of the first element. That is true of arrays but not std:vectors. You get the address of the std::vector object.

The reason why I changed the vector pointer to a reference is because passing by reference is generally preferred over pointers if you simply want to avoid the copy.

Soaps wrote:
Since in my main() there is no string variable holding the name (it is just hard-coded into the call) to be passed, I didn't know that I could use a reference for it. Unless I am way off, what is that string& referencing?


Good question!

Because your parameter type is const std::string& then I think the the const char* you supplied in the call will create a const std::string temporary object and the reference will refer to that.

So, as it happens, in this instance passing by reference doesn't save you anything. But if you had passed a std::string them it would have.

Alright Galik, that all makes sense. I did know the reference to vector was the address of the object rather than the first element, not quite sure why I worded it like that.

I had never heard of the temporary objects, I actually thought that changing that string parameter to a reference was going to give me an error since there was no variable to begin with.

Thanks again guys, I look forward to chatting again as I run into problems over the next couple weeks.
Topic archived. No new replies allowed.