One Container question 
July 22nd, 2005, 11:23 AM
| | | One Container question
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 | 
July 22nd, 2005, 11:23 AM
| | | Re: One Container question
"Pat" <Pat@Pat.com> wrote in news:40ad7b43$1_1@rain.i-cable.com:
[color=blue]
> 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[/color]
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 | 
July 22nd, 2005, 11:23 AM
| | | Re: One Container question
X is double number.
"Gregg" <gregg@invalid.invalid> ¦b¶l¥ó
news:Xns94F035F81D8gregginvalidinvalid@207.69.154. 203 ¤¤¼¶¼g...[color=blue]
> "Pat" <Pat@Pat.com> wrote in news:40ad7b43$1_1@rain.i-cable.com:
>[color=green]
> > 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[/color][/color]
larger.[color=blue][color=green]
> >
> > Thanks. Pat[/color]
>
> 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[/color] | 
July 22nd, 2005, 11:23 AM
| | | Re: One Container question
"Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
[color=blue]
> X is double number.[/color]
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 | 
July 22nd, 2005, 11:23 AM
| | | Re: One Container question
"Gregg" <gregg@invalid.invalid> wrote in message
news:Xns94F0A46AF65Egregginvalidinvalid@207.69.154 .203...[color=blue]
> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
>[color=green]
> > X is double number.[/color]
>
> Then just use a std::set<double>[/color]
And change
x.push_back(d);
to
x.insert(d);
john | 
July 22nd, 2005, 11:23 AM
| | | Re: One Container question
Gregg wrote:[color=blue]
> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
>
>[color=green]
>>X is double number.[/color]
>
>
> 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.[/color]
Do you mean std::map<double, int>? | 
July 22nd, 2005, 11:24 AM
| | | Re: One Container question
Gregg wrote:
[color=blue]
> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
>
>[color=green]
>>X is double number.[/color]
>
>
> 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[/color]
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 | 
July 22nd, 2005, 11:25 AM
| | | Re: One Container question
Paul <Paul@Paul.net> wrote in
news:c5f2af53d16201ad040272cdabd901c6@news.1usenet .com:
[color=blue]
> Gregg wrote:
>[color=green]
>> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
>>
>>[color=darkred]
>>>X is double number.[/color]
>>
>>
>> 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[/color]
>
> 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
>[/color]
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 | 
July 22nd, 2005, 11:25 AM
| | | Re: One Container question
Jeff Schwab <jeffplus@comcast.net> wrote in
news:saSdnToXmuWKejDdRVn-hw@comcast.com:
[color=blue]
> Gregg wrote:[color=green]
>> "Pat" <Pat@Pat.com> wrote in news:40ad879b$1_2@rain.i-cable.com:
>>
>>[color=darkred]
>>>X is double number.[/color]
>>
>>
>> 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.[/color]
>
> Do you mean std::map<double, int>?[/color]
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 | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,662 network members.
|