Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 778
  • Last Modified:

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?
0
steenpat
Asked:
steenpat
  • 2
2 Solutions
 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now