Associative Container unsorted

I am running into problems because I am currently using std::map<string, string> because I need a unique key but I don't want it to be sorted like it is internally, I need the order based on the insertion order. I can't seem to find a way to do this other than vector<pair<string, string>> and of course I need to use find to see if the key is already there or not, which is a pain and a performance problem. Is there a better solution to this problem?
steenpatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
An unsorted associative container ? That sounds horrible (performance wise).

Why is it that you need the data in insertion order ? Can you elaborate on your requirements ? What kind of lookups do you need to be able to do ?
0
jkrCommented:
The isea about using a vector sounds good - but *together* with the map. You could just store references to the map's values (or even better: the keys) there to maintain the insertion order and use the map to ensure uniqueness.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
steenpatAuthor Commented:
"An unsorted associative container ? That sounds horrible (performance wise)."

It does sound horrible. What I need is a container that has a file path and a unique page name. However the requirements are to list them in order in which they are found (files that are scanned). I don't really need to lookup anything actually I am have to iterate through every element and process it/display it etc.

The problem is the page names cannot be duplicates if so they have to have something like (2), (3) appended to the end. The map is handy because when I insert it will return if it was successful or not since I'm using the page name as a key. Using a vector I have to use find to find to see if its a duplicate which is very costly. I cannot use the file path as a key because there are situations in which the pages are multipage and use the same filename.

"The isea about using a vector sounds good - but *together* with the map. You could just store references to the map's values (or even better: the keys) there to maintain the insertion order and use the map to ensure uniqueness."

I guess that sounds like a plan then. I'll see hwo it works out or if infinity08 has a better solution.
0
Infinity08Commented:
>> I don't really need to lookup anything actually I am have to iterate through every element and process it/display it etc.

Then you don't need an associative container. A simple vector is enough.


>> The problem is the page names cannot be duplicates

You can keep a set of the path names only next to the vector :
typedef std::string FilePath;
typedef std::string UniquePageName;
std::vector<std::pair<FilePath, UniquePageName> > container;
std::set<FilePath> containerUnique;
 
// on insertion :
std::pair<FilePath, UniquePageName> newData;
if (containerUnique.find(newData.first) != containerUnique.end()) {
  containerUnique.insert(newData.first);
  container.push_back(newData);
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.