本文整理汇总了C++中LinkedHashMap::values方法的典型用法代码示例。如果您正苦于以下问题:C++ LinkedHashMap::values方法的具体用法?C++ LinkedHashMap::values怎么用?C++ LinkedHashMap::values使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LinkedHashMap
的用法示例。
在下文中一共展示了LinkedHashMap::values方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testValuesIterator
void LinkedHashMapTest::testValuesIterator() {
LinkedHashMap<int, std::string> map;
populateMap(map);
int count = 0;
Pointer< Iterator<std::string> > iterator(map.values().iterator());
while (iterator->hasNext()) {
std::string value = iterator->next();
CPPUNIT_ASSERT_EQUAL(Integer::toString(count), value);
count++;
}
CPPUNIT_ASSERT_MESSAGE("Iterator didn't cover the expected range", count++ == MAP_SIZE);
iterator.reset(map.values().iterator());
CPPUNIT_ASSERT_THROW_MESSAGE(
"Should throw an IllegalStateException",
iterator->remove(),
IllegalStateException);
count = 0;
while (iterator->hasNext()) {
iterator->next();
iterator->remove();
count++;
}
CPPUNIT_ASSERT_MESSAGE("Iterator didn't remove the expected range", count++ == MAP_SIZE);
CPPUNIT_ASSERT_THROW_MESSAGE(
"Should throw an IllegalStateException",
iterator->remove(),
IllegalStateException);
}
示例2: testOrderedValues
void LinkedHashMapTest::testOrderedValues() {
int i;
int size = 100;
{
LinkedHashMap<int, int> map;
for (i = 0; i < size; i++) {
map.put(i, i * 2);
}
Collection<int>& set = map.values();
Pointer< Iterator<int> > iter(set.iterator());
CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size 1", map.size() == set.size());
for (i = 0; iter->hasNext(); i++) {
int value = iter->next();
CPPUNIT_ASSERT_EQUAL_MESSAGE("Returned incorrect values set 1", value, i * 2);
}
}
LinkedHashMap<int, int> map2(200, .75f, true);
for (i = 0; i < size; i++) {
map2.put(i, i * 2);
}
Collection<int>& set = map2.values();
Pointer< Iterator<int> > iter(set.iterator());
CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size 2", map2.size() == set.size());
for (i = 0; i < size && iter->hasNext(); i++) {
int value = iter->next();
CPPUNIT_ASSERT_EQUAL_MESSAGE("Returned incorrect values set 2", value, i * 2);
}
/* fetch the even numbered entries to affect traversal order */
int p = 0;
for (i = 0; i < size; i += 2) {
p = p + map2.get(i);
}
CPPUNIT_ASSERT_EQUAL_MESSAGE("invalid sum of even numbers", 2450 * 2, p);
set = map2.values();
iter.reset(set.iterator());
CPPUNIT_ASSERT_MESSAGE("Returned set of incorrect size 3", map2.size() == set.size());
for (i = 1; i < size && iter->hasNext(); i += 2) {
int value = iter->next();
CPPUNIT_ASSERT_EQUAL_MESSAGE("Returned incorrect values set 3", value, i * 2);
}
for (i = 0; i < size && iter->hasNext(); i += 2) {
int value = iter->next();
CPPUNIT_ASSERT_EQUAL_MESSAGE("Returned incorrect values set 4", value, i * 2);
}
CPPUNIT_ASSERT_MESSAGE("Entries left to iterate on", !iter->hasNext());
}
示例3: testValues
void LinkedHashMapTest::testValues() {
LinkedHashMap<int, std::string> hashMap;
populateMap(hashMap);
Collection<std::string>& c = hashMap.values();
CPPUNIT_ASSERT_MESSAGE("Returned collection of incorrect size()", c.size() == hashMap.size());
for (int i = 0; i < MAP_SIZE; i++) {
CPPUNIT_ASSERT_MESSAGE("Returned collection does not contain all keys",
c.contains(Integer::toString(i)));
}
c.remove("10");
CPPUNIT_ASSERT_MESSAGE("Removing from collection should alter Map",
!hashMap.containsKey(10));
}
示例4: foreachpair
TEST(LinkedHashMapTest, Foreach)
{
LinkedHashMap<string, int> map;
map["foo"] = 1;
map["bar"] = 2;
map["caz"] = 3;
map["foo"] = 4; // Re-insert a key.
list<string> keyList = map.keys();
list<int> valueList = map.values();
vector<string> keys{keyList.begin(), keyList.end()};
vector<int> values{valueList.begin(), valueList.end()};
{
int i = 0;
foreachpair (const string& key, int value, map) {
EXPECT_EQ(keys[i], key);
EXPECT_EQ(values[i], value);
i++;
}
}