std vector problem

Started by
10 comments, last by Zeblar Nagrim 22 years, 9 months ago
I want to do something like this with a "std::vector" object:
        

int main(int argc, char* argv[])
{
  int *piData = new int[10];
  
  for(int i=0; i<10; i++) piData [i] = 0;

  SetDataValue(piData +5);

  return 0;
}

void::SetDataValue(int *pData)
{
  *pData   = 1;
  *pData+1 = 2;
  *pData+2 = 3;
}

    
Here is the same thing but with a std int vector (this dosn´t work, I get this error: error C2676: binary '+' : 'class std::vector >' does not define this operator or a conversion to a type acceptable to the predefined operator):
  

int main(int argc, char* argv[])
{
  std::vector <int> m_vData;
  
  for(int i=0; i<10; i++) vData.push_back(0);

  // Dont work. Why?

  SetDataValue(m_vData+5);

  return 0;
}

void::SetDataValue(int *pData)
{
  *pData   = 1;
  *pData+1 = 2;
  *pData+2 = 3;
}

  
Thanks, Zeblar Nagrim, Lord of Chaos Edited by - Zeblar Nagrim on June 30, 2001 4:30:21 PM Edited by - Zeblar Nagrim on June 30, 2001 4:36:58 PM Edited by - Zeblar Nagrim on June 30, 2001 4:39:51 PM
Advertisement
The reason it won''t work is because the std::vector hides the implementation. Not to sound condescending, but the code example you show below doesn''t even make sense, in light of how one would use the std templates. An std::vector can''t be used with "side effects" like you normally get in regular C code, which is what you are trying to do.

To clarify further, consider this declaration:

std::vector vData;

''vData'' is NOT a int type. It is a std::vector, does that make sense?

The vData variable should be referenced like:

vData[0]=1;
vData[1]=2;
vData[2]=3;

Hope this helps some.

Dean M.
Think OO, first the object then the message/ the method.

Instead of:

push_back.vData(0);

you need to call:

m_vData.push_back(0);

======= ^^^^^^^^^
object method



Bjoern
... and to initialize the vector you should write:

std::vector m_vData(10, 0);

And your SetDataValue should definetly ake another parameter type if you want to pass a vector into it. A vector might be implemented using an array but this isn''t a necessity and you shouldn''t rely on it.

What might work is to pass an iterator into your function.

SetDataValue(m_vData.begin()+5);

Perhaps you have luck and the vector iterator is implemented as a normal pointer. However this solution is highly unportable and not future proof.

Bjoern
Thank u bjoern! That SetDataValue(m_vData.begin()+5); thing works!
It works, but that can change with the next release of the compiler and the stl api delivered with it. And it might not be portable.

Bjoern
From Item 16 in "Effective STL" by Scott Meyers (my new favorite book):
quote:
If you travel in the wrong circles, you may run across a shady character who will tell you that you can use v.begin () in place of &v[0] because (these loathsome creatures will tell you) begin returns an iterator into the vector, and for vectors, iterators are really pointers. That's often true, but...it's not always true, and you should never rely on it...Frankly, if you're hanigng out with people who tell you to use v.begin () instead of &v[0], you need to rethink your social circle.


Use &m_vData[0] + 5 in your code and it should be portable. Or even &m_vData[5].

The elements in a vector are constrained by the C++ Standard to be stored in contiguous memory, so this should always work--unless you have less than 6 elements in your array. You should probably put an assert m_vData.size () >= 6 before the call, just to be safe.

Edited by - Stoffel on July 1, 2001 2:56:56 PM
I am sure that I stated that the usage of an iterator for this solution isn''t the real way to go. The []-operator is far better with the info about the requirement of continuous memory for a vector. Thank you Stoffel.

A better solution would be to define a function with a better interface or a wrapper so you won''t rely oeither on the iterator nor on the []-operator and you wuold only have to change such a usage pattern in your wrapper function if something went wrong or changes.

By the way, I ordered Effective STL but it hasn''t arrived yet. Stoffel, what is your overall impression of this book?

Bjoern
Bjoern:

The book is excellent. As you can tell from my quote, he still hasn''t lost his sense of humor. I just finished reading it (it arrived on Thursday), and I found about 10 of the 50 items that contained new/interesting information for me, which makes it well worth it.

It wasn''t as illuminating as the Effective C++ series for me, but I read those books at a time when I was just learning to use C++, whereas I''ve been doing STL for about a year now professionally. Still, I highly recommend it (just made a top-level post to that effect.)
I really hope that I will get my "Effetive STL" soon. Amazon Germany states that it isn't released yet *brrrr*.

By the way, a successor of "Exceptional C++" will be released in august. "More exceptional c++" is another "must have" for me.

Oh, I have got another question for you Stoffel:

Where do you get the information about the requirement to implement a vector via continuous memory? While looking through "C++ Programming" from Stroustrup I can't find this important piece of information.

Greetings,


Bjoern

Edited by - bjoern on July 5, 2001 4:15:24 AM

This topic is closed to new replies.

Advertisement