One Container question

P

Pat

When inserting an number, say X, into a vector, if X already is in the
vector, then do nothing ( because I do want a repeated X occurs in the
vector). Otherwise, push X into the container.

Any efficient way to do this? Because my vector container is very larger.

Thanks. Pat
 
G

Gregg

Pat said:
When inserting an number, say X, into a vector, if X already is in the
vector, then do nothing ( because I do want a repeated X occurs in the
vector). Otherwise, push X into the container.

Any efficient way to do this? Because my vector container is very larger.

Thanks. Pat

If the type of X has (or can have) a relational operator defined on it,
then you ou can use a std::set<> in parallel with the vector to check for
existence in the set before adding it to the vector. Otherwise, you will
have to do a linear search using std::find. Is it possible you don't need
the vector at all and that what you really want is a set?

Gregg
 
P

Pat

X is double number.
Gregg said:
If the type of X has (or can have) a relational operator defined on it,
then you ou can use a std::set<> in parallel with the vector to check for
existence in the set before adding it to the vector. Otherwise, you will
have to do a linear search using std::find. Is it possible you don't need
the vector at all and that what you really want is a set?

Gregg
 
G

Gregg

Pat said:
X is double number.

Then just use a std::set<double> to keep track of what you've added to the
vector. You might also consider using std::map<int, double> and not use the
vector at all. The suitablility of this depends on how often you will be
looking up entries versus adding them. Perhaps you don't need to look up by
index at all, in which case just use a set and do away with the vector.

Gregg
 
J

Jeff Schwab

Gregg said:
Then just use a std::set<double> to keep track of what you've added to the
vector. You might also consider using std::map<int, double> and not use the
vector at all.

Do you mean std::map<double, int>?
 
P

Paul

Gregg said:
Then just use a std::set<double> to keep track of what you've added to the
vector. You might also consider using std::map<int, double> and not use the
vector at all. The suitablility of this depends on how often you will be
looking up entries versus adding them. Perhaps you don't need to look up by
index at all, in which case just use a set and do away with the vector.

Gregg

The problem with std::set<double> is that double values rarely are
equal. Tests for equality (as std::set::find() does) will yield
inconsistent results.

Paul
 
G

Gregg

The problem with std::set<double> is that double values rarely are
equal. Tests for equality (as std::set::find() does) will yield
inconsistent results.

Paul

I agree in general, but checking for equality was the OP's requirement.
Depending on how the doubles are being generated (e.g., are they being
read from a file that contains dollars and cents amounts), this may or
may not be wise.

Gregg
 
G

Gregg

Do you mean std::map<double, int>?

No, I meant a map that would enable an int to be supplied as a key to look
up a double, so it could serve as a possible substitute for a vector
<double>.

Gregg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top