Go Premium for a chance to win a PS4. Enter to Win


inserting class into a vector

Posted on 2002-07-24
Medium Priority
Last Modified: 2013-12-14
I have a class temp(string, int) and a vector
vector<temp> vec1;

I can create a temp object no problem

temp temp_object("something", 0);

but am unable to insert temp into vec1.


What is going wrong?

Question by:Karls
  • 4
  • 3
  • 3
  • +1

Expert Comment

ID: 7176403
If you want to place objects into a std::vector those objects need to have a copy-constructor:

class temp {
    temp() {};    // standard c'tor
    temp( temp& t ) {};    // copy-c'tor

This is because objects that are inserted are actually copies of the original objects. So be careful if you have pointers in your class as those may become invalid.

Author Comment

ID: 7176455
I have worked it out (10 seconds after I posted it) :( but what i need to know is how would I run a function (already created in the temp class) of the object after it has been inserted into the vector


cout << vec1(1).get_id();

Expert Comment

ID: 7176620

cout << vec1[1].get_id();

or (this version checks that you haven't used an index that doesn't exist in the vector - bounds checking is very useful and recommended unless speed is of utmost importance.  If it's that important to you, you might not be using vector anyway)

cout << vec1.at(1).get_id();
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 7177424
*Sigh* STL is designed with performance in mind. A vector is in general hardly any slower than an array would be. Of course it depends on the implementation -- however, complexity is part of the standard, so there is not that much freedom. I've seen it being used in PC games development excessively and very rarely saw the need to replace it with a proprietary structure. It might hold true on other platforms, especially psx/ps2 call for a non-generic approach most of the time due to stringent limitations. On a PC one shouldn't hope to gain any significant speed by favoring an array over a vector.

Expert Comment

ID: 7177551
Erm, the point I was making was that access via vector::at is slightly slower than vector::operator[].  I was also making the point that generally the speed hit is unimportant (but finite) in comparison to the bugs it can help you find.  

I also said that if you were worried at the difference between at() and [], you might be looking at using arrays anyway.  They *are* more efficient (std::vector is normally based on an array), but in the vast majority of cases, the marginal efficiency gain is not worth it.

>>*Sigh* STL is designed with performance in mind.
I know.  For the record, I write physical simulation software that can run for more than a day before completing.  I also use the STL extensively, and at() by default as access to vectors.  *I have nothing against the STL*.

Karls - sorry for this, I hope the marginal light generated made the heat worthwhile  :-)

Expert Comment

ID: 7177562
Did you, by any chance, look at the assembly listing generated by an optimizing compiler, let's say msvc? Could you then explain to me, how an array could possibly be accessed any faster than the std::vector that comes with v6?

Expert Comment

ID: 7177572
p.s.: I write code that can run 150 micro-seconds before completing, just to point out what I'm talking about...
LVL 30

Expert Comment

ID: 7177732
>>Did you, by any chance, look at the assembly listing
>>generated by an optimizing compiler, let's say msvc?
>>Could you then explain to me, how an array could
>>possibly be accessed any faster than the std::vector
>>that comes with v6?

There is a difference, but 99% of the time, the performance difference is so small, it's not worth worrying about.
However, if you have real time code, and every single machine instruction counts, then you might have to consider using array over vector.

IainHere main point was using operator[] over at() function.  Which there is also very little performance difference, and again, usually not worth worrying about.

It's also hard to create a test in which you can actually measure the differnce.

Expert Comment

ID: 7177767
Karls - ignore the remainder, and apologies for the notification.

fl0yd - the purpose of this site is to help people with their problems.  I explained the purpose of my original post above.  Apologies if you are annoyed in any way, and think that I'm trying to steal some points from you or something; I really do not care about points.  Anyway, back to the featured programme:

I had written a very long discussion of the finer points, but what I'm really saying can be summed up simply.

1) arrays are faster then vectors only in highly specialised applications [and here I'm not talking about *access*, as you inferred], but for most people most of the time there is no significant advantage to be gained.
2) .at() access is a bit slower than [] access, but is still recommended whenever it is remotely possible that you could overrun the bounds of the vector.
3) No hard feelings :-)
LVL 30

Expert Comment

ID: 7177810
>>If you want to place objects into a std::vector those
>>objects need to have a copy-constructor:

I don't believe this is the problem.
First of all if you don't have any constructors in your class, the compiler will automatically create a copy constructor for you.

That real problem is that insert is not being past the proper arguments.

Try the following method:
vec1.insert(vec1.begin(), temp_object);
LVL 30

Accepted Solution

Axter earned 400 total points
ID: 7177819
The vector::insert member function needs to be passed one of the following argument set:

iterator insert(iterator it, const T& x = T());
void insert(iterator it, size_type n, const T& x);
void insert(iterator it, const_iterator first, const_iterator last);

So you need to use an iterator to use insert.  This tells the function where you want to insert it at.  If you just want to add the value to the end of the vector, then you should use push_back instead.

If your purpose is really to insert the arguement some where in the middle of the vector, then you need to tell the function where in the vector you want to insert it at.


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

783 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