testclass.hpp
#pragma once
#include <iostream>
#include <cstddef>
#include <assert.h>
#include <string.h>
#include <vector>
#include <unordered_map>
using namespace std ;
class TestClass{
public:
TestClass(string s_):marketID(s_){}
pair<unordered_map<string,int>::iterator,bool> find(string s) ;
pair<unordered_map<string,int>::iterator,bool> insert(string s,int inum) ;
private:
string marketID ;
unordered_map<string,int> themap ;
} ;
pair<unordered_map<string,int>::iterator,bool> TestClass::find(string s)
{
pair<unordered_map<string,int>::iterator,bool> ret ;
auto got = themap.find( s ) ;
if( got == themap.end() ){
ret = make_pair(got,false) ;
}else{
ret = make_pair(got,true) ;
}
return ret ;
} //find
pair<unordered_map<string,int>::iterator,bool> TestClass::insert(string s,int inum)
{
pair<unordered_map<string,int>::iterator,bool> ret ;
//pair<string,int> mypair = {s,inum} ;
//ret = themap.insert( mypair ) ;
ret = themap.insert( make_pair(s,inum) ) ;
return ret ;
} //insert
test.cpp
#include "testclass.hpp"
using namespace std ;
int main(int argc, char* argv[])
{
string s = "mars" ;
TestClass ts(s) ;
ts.insert( string( "11111" ) ,1 ) ;
ts.insert( string( "21111" ) ,2 ) ;
ts.insert( string( "31111" ) ,3 ) ;
ts.insert( string( "41111" ) ,4 ) ;
auto ret = ts.find(s) ;
if( ret.second ){
auto got = ret.first ;
cout << got->second << endl ;
}else{
cout << "not found" << endl ;
}
s = "31111" ;
ret = ts.find(s) ;
if( ret.second ){
auto got = ret.first ;
cout << got->second << endl ;
got->second += 100 ;
}else{
cout << "not found" << endl ;
}
auto ret2 = ts.find(s) ;
if( ret2.second ){
auto got = ret2.first ;
cout << got->second << endl ;
}else{
cout << "not found" << endl ;
}
cout << "========================" << endl ;
auto ret3 = ts.insert( string( "41111" ) ,100 ) ;
if( ret3.second ){
cout << "insert is fine" << endl ;
}else{
cout << "already in map,the ori val:" << ret3.first->second << endl ;
}
cout << "========================" << endl ;
auto ret4 = ts.insert( string( "51111" ) ,100 ) ;
if( ret4.second ){
cout << "insert is fine" << endl ;
}else{
cout << "already in map,the ori val:" << ret4.first->second << endl ;
}
}
output :
not found
3
103
========================
already in map,the ori val:4
========================
insert is fine
留言列表