-
January 11th, 2004, 09:44 PM
#1
STL vectors vs. maps
I need a collection which will associate a string with an integer. The strings will be added numerically and so the number associated to the first string added will be 0, the second 1, etc. I was thinking I would use a vector, but the problem is that I then want to be able to search the collection for the string and find the integer related to it. A vector does not seem to have an existing search algorithm (am I wrong?). I could write one myself, but I was hoping there was another way.
I also looked at using a map, but I am finding the interface to a map to be needlessly complex. The example I have looked at shows:
theMap.insert(INT2STRING::value_type(0,"Zero"));
where I would rather just use:
theMap.insert(0,"Zero");
and searching the map involves iterators, and all sorts of other needless garbage when I just want to have a method like:
int find(string searchString);
So do I need to write a custom class or method, or is there something I am not seeing.
thanks,
eggman
-
January 11th, 2004, 09:56 PM
#2
You can try MFC CMap or other CMap-favored template classes.
-
January 11th, 2004, 10:22 PM
#3
A vector and a map are different containers. Each is a good solution for certain problems.
Kuphryn
-
January 11th, 2004, 10:32 PM
#4
More info is needed to determine which container is best.
For example, will there be any "missing" numbers ? Or
will they all be included.
Also, it is not clear whether you want the int or the string to
be the key. Your insert had the int as the key, but the
find example that you gave was trying to lookup by string.
Here is a simple xample of both :
Code:
#include <map>
#include <string>
#include <iostream>
using namespace std;
typedef std::map<std::string,int> STRINGTOINT;
typedef STRINGTOINT::iterator ITERATOR;
int main()
{
STRINGTOINT theMap;
ITERATOR it;
// add to the map
theMap["zero"] = 0;
theMap["ten"] = 10;
theMap["three"] = 3;
// search in the map
it = theMap.find("ten");
if (it != theMap.end())
{
cout << it->first << " " << it->second << endl;
// or
cout << "ten" << " " << theMap["ten"] << endl;
}
return 0;
}
//
//
//
#include <map>
#include <string>
#include <iostream>
using namespace std;
typedef std::map<int,std::string> INT2STRING;
typedef INT2STRING::iterator ITERATOR;
int main()
{
INT2STRING theMap;
ITERATOR it;
// add to the map
theMap[0] = "zero";
theMap[10] = "ten";
theMap[3] = "three";
// search in the map
it = theMap.find(0);
if (it != theMap.end())
{
cout << it->first << " " << it->second << endl;
// or
cout << 0 << " " << theMap[0] << endl;
}
return 0;
}
-
January 11th, 2004, 10:53 PM
#5
Technically both could be a key.
There are no missing integers. They will start at 0 and go up in order 0,1,2,3,4 etc. There are also no duplicates, so it could be a key.
There are no duplicate strings either. Each integer maps to a single string. Each string maps to a single integer. For search purposes, I want to be able to supply a string, and receive an integer in return. I can not see any reason why I would want to do the opposite, so it would seem the String should be the key.
eggman
-
January 11th, 2004, 11:46 PM
#6
well, I think with the above examples I can use either map or cmap. Maps still seem to have some irritating necessities, like using an iterator, etc, but it does not look as bad as I initially thought. Anyway, that should do for now. Topic closed.
-
January 12th, 2004, 01:19 AM
#7
Just another point, I too sometimes prefer I different client
interface that fit special needs better. You can create your
own class with the map as a variable and put whatever
type of inteface you want. Something like this:
Code:
#include <map>
#include <string>
#include <iostream>
class NumberName
{
public:
typedef std::pair<std::string,int> PAIR; // needed for some versions of VC++
void insert(const std::string& name, int value)
{
theMap[name] = value;
}
int find(const std::string& name)
{
std::map<std::string,int>::iterator it = theMap.find(name);
if (it != theMap.end())
return it->second;
else
return -1;
}
// or maybe
bool find(const std::string& name , int& value)
{
std::map<std::string,int>::iterator it = theMap.find(name);
if (it != theMap.end())
{
value = it->second;
return true;
}
else
return false;
}
private:
std::map<std::string,int> theMap;
};
using namespace std;
int main()
{
NumberName theMap;
// add to the map
theMap.insert("zero",0);
theMap.insert("one",1);
theMap.insert("two",2);
// search in the map
int i = theMap.find("one");
if (i >= 0)
cout << "found : " << i << endl;
// or using the second method
if (theMap.find("two",i))
cout << "found : " << i << endl;
return 0;
}
Last edited by Philip Nicoletti; January 12th, 2004 at 01:43 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|