int main () { MyMap amap; amap.insert("one", 1); amap.insert("two", 2); amap.insert("three", 3); //amap["three"].push_back(3); //amap["three"].push_back(3); //cout << amap["one"]; amap.find("one"); amap.find("two"); amap.find("three"); return 0; }
int main() { MyMap *mymap = new MyMap(); vector<int> *sum_vec; const Key *key; int len; int p[MaxLen]; srand( unsigned(time(NULL))); for(int c = 0; c < MaxNum; ++c) { len = rand() % MaxLen + 1; int sum = 0; for(int i = 0; i < len; ++i) { p[i] = rand() % MaxNum; sum += p[i]; } key = new Key(p, len); sum_vec = new vector<int>(); sum_vec->push_back(sum); mymap->insert(make_pair(*key, sum_vec)); delete key; } for(MyMap::iterator it = mymap->begin(); it != mymap->end(); ++it) { delete it->second; } delete mymap; return 0; }
void tst_QMap::clear() { { MyMap map; map.clear(); QVERIFY( map.isEmpty() ); map.insert( "key", MyClass( "value" ) ); map.clear(); QVERIFY( map.isEmpty() ); map.insert( "key0", MyClass( "value0" ) ); map.insert( "key0", MyClass( "value1" ) ); map.insert( "key1", MyClass( "value2" ) ); map.clear(); QVERIFY( map.isEmpty() ); } QCOMPARE( MyClass::count, int(0) ); }
bool Test_STL() { TTRACE(_T("=====================================================")); typedef std::map<int, char> MyMap; MyMap map; map.insert(std::make_pair(1, 'A')); map.insert(std::make_pair(2, 'B')); std::for_each(map.begin(), map.end(), pair_second(Printer())); std::for_each(map.begin(), map.end(), pair_second(&Print)); return true; }
int main() { //начальная инициализация map MyMap m; //использование value_type для формирование пары "ключ - значение" m.insert(MyMap::value_type(1,"Ivanov")); //можно непосредственно использовать тип pair m.insert(pair<int,string>(2,"Petrov")); //использование функции make_pair m.insert(make_pair(3,"Sidorov")); for(auto x: m) { //использование свойств итератора: //first для обращения к ключу, //second для обращение к значению cout << x.first << " " << x.second << endl; } return 0; }
int main() { //using namespace std; int number1 = 100; int number2 = 20000; string str1 = IntToString(number1); string str2 = IntToString(number2); string comma(", "); str1 += comma; str1 += str2; cout << str1 << endl; MyMap amap; amap.insert(str1,1); amap.find(str1); const char* strL; strL = str1.c_str(); regex_t preg; size_t nmatch = 3; regmatch_t pmatch[nmatch]; int i, j; string restring1; string restring2; if (regcomp(&preg, "([[:digit:]]+), ([[:digit:]]+)", REG_EXTENDED|REG_NEWLINE) != 0) { printf("regex compile failed.\n"); exit(1); } printf("String = %s\n", strL); if (regexec(&preg, strL, nmatch, pmatch, 0) != 0) { printf("No match.\n"); } else { for (i = 0; i < nmatch; i++) { /* nmatch にマッチした件数が入る */ printf("Match position = %d, %d , str = ", (int)pmatch[i].rm_so, (int)pmatch[i].rm_eo); if (pmatch[i].rm_so >= 0 && pmatch[i].rm_eo >= 0) { for (j = pmatch[i].rm_so ; j < pmatch[i].rm_eo; j++) { //putchar(strL[j]); if(i==1){ restring1+=strL[j]; } if(i==2){ restring2+=strL[j]; } } } printf("\n"); } } regfree(&preg); cout << restring1 <<endl; cout << restring2 <<endl; assert(typeid(restring1) == typeid(string)); assert(typeid(restring2) == typeid(string)); int renumber1,renumber2; istringstream istr1(restring1); istr1 >> renumber1; cout << renumber1 <<endl; assert(typeid(renumber2) == typeid(int)); istringstream istr2(restring2); istr2 >> renumber2; cout << renumber2 <<endl; assert(typeid(renumber2) == typeid(int)); return 0; }
int main () { using namespace boost::interprocess; //Remove shared memory on construction and destruction struct shm_remove { //<- #if 1 shm_remove() { shared_memory_object::remove(test::get_process_id_name()); } ~shm_remove(){ shared_memory_object::remove(test::get_process_id_name()); } #else //-> shm_remove() { shared_memory_object::remove("MySharedMemory"); } ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } //<- #endif //-> } remover; //Shared memory front-end that is able to construct objects //associated with a c-string. Erase previous shared memory with the name //to be used and create the memory segment at the specified address and initialize resources //<- #if 1 managed_shared_memory segment(create_only,test::get_process_id_name(), 65536); #else //-> managed_shared_memory segment (create_only ,"MySharedMemory" //segment name ,65536); //segment size in bytes //<- #endif //-> //Note that map<Key, MappedType>'s value_type is std::pair<const Key, MappedType>, //so the allocator must allocate that pair. typedef int KeyType; typedef float MappedType; typedef std::pair<const int, float> ValueType; //Alias an STL compatible allocator of for the map. //This allocator will allow to place containers //in managed shared memory segments typedef allocator<ValueType, managed_shared_memory::segment_manager> ShmemAllocator; //Alias a map of ints that uses the previous STL-like allocator. //Note that the third parameter argument is the ordering function //of the map, just like with std::map, used to compare the keys. typedef map<KeyType, MappedType, std::less<KeyType>, ShmemAllocator> MyMap; //Initialize the shared memory STL-compatible allocator ShmemAllocator alloc_inst (segment.get_segment_manager()); //Construct a shared memory map. //Note that the first parameter is the comparison function, //and the second one the allocator. //This the same signature as std::map's constructor taking an allocator MyMap *mymap = segment.construct<MyMap>("MyMap") //object name (std::less<int>() //first ctor parameter ,alloc_inst); //second ctor parameter //Insert data in the map for(int i = 0; i < 100; ++i){ mymap->insert(std::pair<const int, float>(i, (float)i)); } return 0; }