Solved

inserting class into a vector

Posted on 2002-07-24
11
221 Views
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.

vec1.insert(temp_object);

What is going wrong?

0
Comment
Question by:Karls
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 8

Expert Comment

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

class temp {
public:
    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.
0
 

Author Comment

by:Karls
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

ie.

cout << vec1(1).get_id();
0
 
LVL 4

Expert Comment

by:IainHere
ID: 7176620
either

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();
0
 
LVL 8

Expert Comment

by:fl0yd
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.
0
 
LVL 4

Expert Comment

by:IainHere
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  :-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:fl0yd
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?
0
 
LVL 8

Expert Comment

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

Expert Comment

by:Axter
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.
0
 
LVL 4

Expert Comment

by:IainHere
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 :-)
0
 
LVL 30

Expert Comment

by:Axter
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);
0
 
LVL 30

Accepted Solution

by:
Axter earned 100 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.
Example:
vec1.push_back(temp_object);

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.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

914 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

16 Experts available now in Live!

Get 1:1 Help Now