Skip to content

Instantly share code, notes, and snippets.

@pcyu16
Created June 22, 2010 11:03
Show Gist options
  • Select an option

  • Save pcyu16/448333 to your computer and use it in GitHub Desktop.

Select an option

Save pcyu16/448333 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <map>
#include <string>
using namespace std;
map<string,int> table;
void print()
{
map<string,int>::iterator it;
cout << "table contents:" << endl;
for(it=table.begin();it!=table.end();it++)
cout << "name: " << it->first << ", val:" << it->second << endl;
}
int main()
{
int cnt=0, times;
string str;
table.clear();
// insert
times=2;
while(times--){
cout << "input id >> ";
cin >> str;
table[str] = ++cnt;
}
print();
// query
cout << endl;
times = 2;
while(times--){
cout << "query >> ";
cin >> str;
if( table.find(str) != table.end() )
cout << "found: " << str << ' ' << table[str] << endl;
else
cout << "cannot find " << str << endl;
}
// delete
cout << endl;
cout << "delete >> ";
cin >> str;
table.erase(str);
print();
table.clear();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment