Look for appropriate data structure

Hello,
I have the following question:
I need to implement some task that takes a pair of strings, and do a dual search,
the first search is according to the first string, and if it was found there is a second search according to the second string.

For example, if the pair is <Ford, shield number> I need to first search some vector of cars name (with key "Ford"), and then search all the list of Ford to see if the shield number I got is there, and if not insert it to the Ford list.

I don't really know what is the best data structure to do it...a vector of pointers to list?

Or perhaps some map\set? the map<pair<string,string>> seem pretty complicated (For now, I don't use c++11). So if you could please suggest me how to do it in the simpiest way I will be gratefull (It's also possible that the first string is not yet in the vector..in this case I should insert it first, and then the shield).
Well if you have to search for a shield number under ford, an easy way is to use a map and a set. This will force a unique shield number for each string. I think that's what you were looking for...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <map>
#include <set>
#include <iostream>


int main()
{
    typedef std::map<std::string, std::set<int>> Data;
    typedef std::set<int> Shield;

    Data x;
    Shield y {123, 5436, 765, 12543};
    Shield z {0x123, 0x5436, 0x765, 0x12543};
    x.insert(std::make_pair("Ford", y));
    x.insert(std::make_pair("Henry", z));

    Data::iterator it = x.find("Ford");
    if (it != x.end())
    {
        for (Shield::iterator ti = it->second.begin(); ti != it->second.end(); ++ti)
            std::cout << *ti << std::endl;
    }
    return 0;
}   


Since you don't use C++11 you'll have to use ::iterator ti instead of auto &ti : x.
Thanks for the quick answer!
But perhaps I didn't gave a perfect example, because as I said, I need the shield to be a string also (perhaps a better example is a singer name as the first string, and a song of his as the second string (then if the song in not in the singer list insert it, and otherwise do nothing..). How does your code change if the set contains string instead of int? thanks
I think all that differs is the int which turns into std::string in the set. Try messing around, it won't hurt!
Great, I'll do it:)
Just another short question...is there a way to use the find function on the second component as well? something like:

if (it != My_Pair.end())
{
if( Payment_ID::iterator p=Payment.find(payment_id)
....
}
where the function declaration is:

bool check_payment_id(string & prefix ,string & payment_id)

Data My_Pair is the map and Payment_ID Payment is the set.

thanks!
Topic archived. No new replies allowed.