473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

One Container question

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
Jul 22 '05 #1
8 1055
"Pat" <Pa*@Pat.com> wrote in news:40**********@rain.i-cable.com:
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
Jul 22 '05 #2
Pat
X is double number.
"Gregg" <gr***@invalid.invalid> ¦b¶l¥ó
news:Xn*******************************@207.69.154. 203 ¤¤¼¶¼g...
"Pat" <Pa*@Pat.com> wrote in news:40**********@rain.i-cable.com:
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

Jul 22 '05 #3
"Pat" <Pa*@Pat.com> wrote in news:40**********@rain.i-cable.com:
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
Jul 22 '05 #4

"Gregg" <gr***@invalid.invalid> wrote in message
news:Xn********************************@207.69.154 .203...
"Pat" <Pa*@Pat.com> wrote in news:40**********@rain.i-cable.com:
X is double number.


Then just use a std::set<double>


And change

x.push_back(d);

to

x.insert(d);

john
Jul 22 '05 #5
Gregg wrote:
"Pat" <Pa*@Pat.com> wrote in news:40**********@rain.i-cable.com:

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.


Do you mean std::map<double, int>?
Jul 22 '05 #6
Gregg wrote:
"Pat" <Pa*@Pat.com> wrote in news:40**********@rain.i-cable.com:

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


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
Jul 22 '05 #7
Paul <Pa**@Paul.net> wrote in
news:c5******************************@news.1usenet .com:
Gregg wrote:
"Pat" <Pa*@Pat.com> wrote in news:40**********@rain.i-cable.com:

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


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
Jul 22 '05 #8
Jeff Schwab <je******@comcast.net> wrote in
news:sa********************@comcast.com:
Gregg wrote:
"Pat" <Pa*@Pat.com> wrote in news:40**********@rain.i-cable.com:

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.


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
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: kazio | last post by:
Hello, So, I need to have double linked, circular list (last element point to the first one and first one points to the last one). I thought maybe I could use list container from STL, but...
1
by: Wolfgang Lipp | last post by:
my question is: do we need container elements for repeating elements in data-centric xml documents? or is it for some reason very advisable to introduce containers in xml documents even where not...
4
by: Ulrich Sprick | last post by:
Hi all, (DB2 V7.1 for WinNT) I am looking for a way to determine the free space in my tablespace (containers), but I can't find out. The tablespace in question is a system managed tablespace in...
3
by: jignesh shah | last post by:
Hi all, Is there a way to recover a single container if its been corrupted or mark bad without restoring whole tablespace? environment: db28.1/aix5.1/tsm/rs-6000. Regards Jignesh
1
by: Don Hames | last post by:
I have a windows application that has a split container in the client area. In the left panel, I added controls via the designer in VS 2005. In the right panel, I want to dynamically create and...
7
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
2
by: Daniel Lipovetsky | last post by:
I would like for an object to "report" to a container object when a new instance is created or deleted. I could have a container object that is called when a new instance is created, as below. ...
18
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
36
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
3
by: Rob McDonald | last post by:
I am interested in having a container which has properties of both the STL's list and vector. (I want my cake and to eat it too). In my application, I will need to add/remove items from arbitrary...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.