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
        IntMap multiplyMap(const IntMap& m, int f)
        {
            IntMap ret;
            for (IntMap::const_iterator it = m.begin(); it != m.end(); ++it)
            {
                ret[it->first] = it->second * f;
            }

            return ret;
        }
Exemple #3
0
  void write()
  {
    Json::Value v;

    for(IntMap::iterator it = ratings.begin();
        it != ratings.end(); it++)
      {
        v[it->first] = it->second;
      }

    std::string filename = get.getPath("ratings.json");
    writeJson(filename, v);
  }
Exemple #4
0
        vector<int> topKFrequent(vector<int>& nums, int k) {
            typedef std::map<int, int> IntMap;
            typedef IntMap::iterator IMIter;

            IntMap countMap;
            for(size_t i = 0, sz = nums.size(); i < sz; ++i)
            {
                if(countMap.find(nums[i]) != countMap.end())
                {
                    countMap[nums[i]] = countMap[nums[i]] + 1;
                }
                else
                {
                    countMap[nums[i]] = 1;
                }
            }

            typedef std::map<int, std::vector<int>* > OrderMap;
            typedef OrderMap::iterator OMIter;
            typedef OrderMap::reverse_iterator OMRter;
            OrderMap orderMap;
            for(IMIter itr = countMap.begin(), end = countMap.end(); itr != end; ++itr)
            {
                if(orderMap.find(itr->second) != orderMap.end())
                {
                    orderMap[itr->second]->push_back(itr->first);
                }
                else
                {
                    std::vector<int>* vec = new std::vector<int>();
                    vec->push_back(itr->first);
                    orderMap[itr->second] = vec;
                }
            }

            std::vector<int> result;
            int count = 0;
            for(OMRter itr = orderMap.rbegin(), end = orderMap.rend(); itr != end && count != k; ++itr)
            {
                std::vector<int>* vec = itr->second;
                for(size_t i = 0, sz = vec->size(); i < sz && count != k; ++i, ++count)
                {
                    result.push_back((*vec)[i]);
                }
            }

            return result;
        }
Exemple #5
0
void HashMapTest::testConstIterator()
{
	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));
	}
	
	std::map<int, int> values;
	IntMap::ConstIterator it = hm.begin();
	while (it != hm.end())
	{
		assert (values.find(it->first) == values.end());
		values[it->first] = it->second;
		++it;
	}
	
	assert (values.size() == N);
}
Exemple #6
0
void HashMapTest::testIterator()
{
	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));
	}
	
	std::map<int, int> values;
	IntMap::Iterator it; // do not initialize here to test proper behavior of uninitialized iterators
	it = hm.begin();
	while (it != hm.end())
	{
		assert (values.find(it->first) == values.end());
		values[it->first] = it->second;
		++it;
	}
	
	assert (values.size() == N);
}
Exemple #7
0
IntMap::const_iterator utl::findValue (const IntMap& iMap, int iVal)
{
  return std::find_if(iMap.begin(),iMap.end(),cmpInt(iVal));
}