Exemple #1
0
void test_erase_map() {
    IntMap map;
    for (int i = 0; i < 100; ++i) {
        map[i] = i + 10000;
    }

    std::cout << "map_size: " << map.size() << std::endl;

    for (IntMap::iterator it = map.begin(); it != map.end(); ++it) {
        std::cout << it->first << ": "  << it->second << std::endl;

        if (it->first % 2 == 0) {
            map.erase(it->first);
        }
    }

    std::cout << "clean finish" << std::endl;
    std::cout << map.size() << std::endl;

    for (IntMap::iterator it = map.begin(); it != map.end(); ++it) {
        std::cout << it->first << ": "  << it->second << std::endl;
    }

    std::cout << map.size() << std::endl;
}
Exemple #2
0
void HashMapTest::testErase()
{
	const int N = 1000;

	typedef HashMap<int, int> IntMap;
	IntMap hm;

	for (int i = 0; i < N; ++i)
	{
		hm.insert(IntMap::ValueType(i, i*2));
	}
	assert (hm.size() == N);
	
	for (int i = 0; i < N; i += 2)
	{
		hm.erase(i);
		IntMap::Iterator it = hm.find(i);
		assert (it == hm.end());
	}
	assert (hm.size() == N/2);
	
	for (int i = 0; i < N; i += 2)
	{
		IntMap::Iterator it = hm.find(i);
		assert (it == hm.end());
	}
	
	for (int i = 1; i < N; i += 2)
	{
		IntMap::Iterator it = hm.find(i);
		assert (it != hm.end());
		assert (*it == i);
	}

	for (int i = 0; i < N; i += 2)
	{
		hm.insert(IntMap::ValueType(i, i*2));
	}
	
	for (int i = 0; i < N; ++i)
	{
		IntMap::Iterator it = hm.find(i);
		assert (it != hm.end());
		assert (it->first == i);
		assert (it->second == i*2);		
	}
}