本文整理汇总了C++中LinkedHashMap::get方法的典型用法代码示例。如果您正苦于以下问题:C++ LinkedHashMap::get方法的具体用法?C++ LinkedHashMap::get怎么用?C++ LinkedHashMap::get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LinkedHashMap
的用法示例。
在下文中一共展示了LinkedHashMap::get方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testRemove
void LinkedHashMapTest::testRemove() {
{
LinkedHashMap<int, std::string> hashMap;
populateMap(hashMap);
int size = hashMap.size();
CPPUNIT_ASSERT_NO_THROW_MESSAGE("Remove returned incorrect value", hashMap.remove(9));
CPPUNIT_ASSERT_THROW_MESSAGE(
"Should have thrown a NoSuchElementException on get of non-existent key.",
hashMap.get(9),
NoSuchElementException);
CPPUNIT_ASSERT_MESSAGE("Failed to decrement size", hashMap.size() == (size - 1));
CPPUNIT_ASSERT_THROW_MESSAGE(
"Should have thrown a NoSuchElementException on remove of non-existent key.",
hashMap.remove(9),
NoSuchElementException);
}
{
LinkedHashMap<int, std::string> hashMap;
for (int i = 0; i < 8192; i++) {
hashMap.put(i, "const");
}
for (int i = 0; i < 8192; i++) {
hashMap.put(i, Integer::toString(i));
}
for (int i = 8191; i >= 0; i--) {
std::string iValue = Integer::toString(i);
CPPUNIT_ASSERT_MESSAGE(std::string("Failed to replace value: ") + iValue,
hashMap.containsValue(iValue));
hashMap.remove(i);
CPPUNIT_ASSERT_MESSAGE(std::string("Failed to remove same value: ") + iValue,
!hashMap.containsValue(iValue));
}
}
{
// Ensure keys with identical hashcode are stored separately and removed correctly.
LinkedHashMap<MyKey, std::string> map;
// Put non-equal object with same hashcode
MyKey aKey;
CPPUNIT_ASSERT(!map.containsKey(aKey));
map.put(aKey, "value");
MyKey aKey2;
CPPUNIT_ASSERT_THROW_MESSAGE(
"Should have thrown NoSuchElementException",
map.remove(aKey2),
NoSuchElementException);
MyKey aKey3;
map.put(aKey3, "foobar");
CPPUNIT_ASSERT_EQUAL(std::string("foobar"), map.get(aKey3));
CPPUNIT_ASSERT_EQUAL(std::string("value"), map.get(aKey));
map.remove(aKey);
map.remove(aKey3);
CPPUNIT_ASSERT(!map.containsKey(aKey));
CPPUNIT_ASSERT(map.isEmpty());
}
}
示例2: testClear
void LinkedHashMapTest::testClear() {
LinkedHashMap<int, std::string> hashMap;
hashMap.put(1, "one");
hashMap.put(3, "three");
hashMap.put(2, "two");
hashMap.clear();
CPPUNIT_ASSERT_EQUAL_MESSAGE("Clear failed to reset size", 0, hashMap.size());
for (int i = 0; i < 125; i++) {
CPPUNIT_ASSERT_THROW_MESSAGE(
"Failed to clear all elements",
hashMap.get(i),
NoSuchElementException);
}
// Check clear on a large loaded map of Integer keys
LinkedHashMap<int, std::string> map;
for (int i = -32767; i < 32768; i++) {
map.put(i, "foobar");
}
map.clear();
CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to reset size on large integer map", 0, map.size());
for (int i = -32767; i < 32768; i++) {
CPPUNIT_ASSERT_THROW_MESSAGE(
"Failed to clear all elements",
map.get(i),
NoSuchElementException);
}
}
示例3: testGet
void LinkedHashMapTest::testGet() {
LinkedHashMap<int, std::string> hashMap;
CPPUNIT_ASSERT_THROW_MESSAGE(
"Should have thrown NoSuchElementException",
hashMap.get(1),
NoSuchElementException);
hashMap.put(22, "HELLO");
CPPUNIT_ASSERT_EQUAL_MESSAGE("Get returned incorrect value for existing key",
std::string("HELLO"), hashMap.get(22));
}
示例4:
TEST(LinkedHashmapTest, Erase)
{
LinkedHashMap<string, int> map;
map["foo"] = 1;
map["bar"] = 2;
ASSERT_EQ(2, map.size());
ASSERT_EQ(1, map.erase("foo"));
ASSERT_EQ(0, map.erase("caz")); // Non-existent key.
ASSERT_NONE(map.get("foo"));
ASSERT_EQ(1, map.size());
ASSERT_SOME_EQ(2, map.get("bar"));
}
示例5: testConstructorMap
void LinkedHashMapTest::testConstructorMap() {
LinkedHashMap<int, int> myMap;
for (int counter = 0; counter < 125; counter++) {
myMap.put(counter, counter);
}
LinkedHashMap<int, int> hashMap(myMap);
for (int counter = 0; counter < 125; counter++) {
CPPUNIT_ASSERT_MESSAGE("Failed to construct correct LinkedHashMap",
myMap.get(counter) == hashMap.get(counter));
}
}
示例6: testPut
void LinkedHashMapTest::testPut() {
{
LinkedHashMap<std::string, std::string> hashMap(101);
hashMap.put("KEY", "VALUE");
CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to install key/value pair",
std::string("VALUE"), hashMap.get("KEY"));
}
{
// Check my actual key instance is returned
LinkedHashMap<int, std::string> map;
for (int i = -32767; i < 32768; i++) {
map.put(i, "foobar");
}
int myKey = 0;
// Put a new value at the old key position
map.put(myKey, "myValue");
CPPUNIT_ASSERT(map.containsKey(myKey));
CPPUNIT_ASSERT_EQUAL(std::string("myValue"), map.get(myKey));
bool found = false;
Set<int>& intSet = map.keySet();
Pointer< Iterator<int> > itr(intSet.iterator());
while (itr->hasNext()) {
int key = itr->next();
found = (key == myKey);
if (found) {
break;
}
}
CPPUNIT_ASSERT_MESSAGE("Should find new key instance in hashashMap", found);
// Add a new key instance and check it is returned
CPPUNIT_ASSERT_NO_THROW(map.remove(myKey));
map.put(myKey, "myValue");
CPPUNIT_ASSERT(map.containsKey(myKey));
CPPUNIT_ASSERT_EQUAL(std::string("myValue"), map.get(myKey));
itr.reset(intSet.iterator());
while (itr->hasNext()) {
int key = itr->next();
found = (key == myKey);
if (found) {
break;
}
}
CPPUNIT_ASSERT_MESSAGE("Did not find new key instance in hashashMap", found);
}
{
// Ensure keys with identical hashcode are stored separately
LinkedHashMap<MyKey, std::string> map;
// Put non-equal object with same hashcode
MyKey aKey;
CPPUNIT_ASSERT(!map.containsKey(aKey));
map.put(aKey, "value");
MyKey aKey2;
CPPUNIT_ASSERT_THROW_MESSAGE(
"Should have thrown NoSuchElementException",
map.remove(aKey2),
NoSuchElementException);
MyKey aKey3;
map.put(aKey3, "foobar");
CPPUNIT_ASSERT_EQUAL(std::string("foobar"), map.get(aKey3));
CPPUNIT_ASSERT_EQUAL(std::string("value"), map.get(aKey));
}
}