通过常量std::映射进行搜索
Searching through a const std::map
我正在处理我的一个类,遇到了一个绊脚石。我将给你一个我的源代码示例——只是类、方法和变量名的名称不同,但实现是相同的。你会看到我的问题,问题&代码块中的关注点。
MyClass.h
#ifndef MY_CLASS_H
#define MY_CLASS_H
const std::string strOne = std::string( "one" );
const std::string strTwo = std::string( "two" );
const std::string strThree = std::string( "three" );
const std::string strUnknown = std::string( "unknown" );
enum Count {
ONE,
TWO,
THREE,
UNKNOWN
};
class MyClass {
private:
const std::map<Count, const std::string> m_mCount = createCountMap();
unsigned short m_uCount;
std::vector<std::string> m_vItems;
std::multimap<const std::string, const std::string> m_mmAssociatedItems;
public:
MyClass();
MyClass( const std::string strItem1, const std::string strItem2 );
static MyClass* get();
void addItem( Count type, const std::string& strItem2 );
void addItem( const std::string& strItem1, const std::string& strItem2 );
private:
static const std::map<Count, const std::string> createCountMap();
};
#endif // MY_CLASS_H
MyClass.cpp
#include "stdafx.h"
#include "MyClass.h"
static MyClass* s_pMyClass = nullptr;
const std::map<Count, const std::string> MyClass:createCountMap() {
std::map<Count, const std::string> m;
m.insert( std::make_pair( Count::ONE, strOne ) );
m.insert( std::make_pair( Count::TWO, strTwo ) );
m.insert( std::make_pair( Count::Three, strThree ) );
m.insert( std::make_pair( Count::UNKNOWN, strUnknown ) );
return m;
} // createCountMap
MyClass* MyClass::get() {
if ( !s_pMyClass ) {
return nullptr;
}
return s_pMyClass;
} // get
MyClass::MyClass() : m_uCount( 0 ) {
m_vItems.clear();
m_mmAssociatedItems.clear();
} // MyClass
MyClass::MyClass( const std::string& strItem1, const std::string& strItem2 ) :
m_uCount( 0 ) {
addItem( strItem1, strItem2 );
} // MyClass
void MyClass::addItem( Count type, const std::string& strItem2 ) {
const std::map<Count, const std::string>::const_iterator it = m_mCount.find( type );
if ( it == m_mCount.end() ) {
// Did not find a valid item key!
// Throw Exception Here!
}
m_vItems.push_back( strItem2 );
m_mmAssociatedItems.insert( std::make_pair( it->second, m_vItems.at( m_uCount ) ) );
++m_uCount;
}
void MyClass::addItem( const std::string& strItem1, const std::string& strItem2 ) {
// I need to do a similar thing as above instead of looking through my
// const std::map at the key values for a match, with this overloaded
// function call I need to use strItem1 as the search item to see if it
// is within the contents of this map which would be the map's ->second
// value. If not throw a similar error as above otherwise once it is
// found populate my vector and multimap same as above and increment
// my count variable.
// This would logically be my next step
const std::map<Count, const std::string>::const_iterator it = m_mCount.begin();
// It Is Within This For Loop That It Fails To Compile! It
// Fails At The it++ Part! Is There A Way Around This? Or
// Am I Missing Something Simple?
for ( ; it != m_mCount.end(); it++ ) {
// If strItem1 == it->second && it != m_mCount.end()
// found it, add items, if not throw error
}
m_vItems.push_back( strItem2 );
m_mmAssociatedItems.insert( std::make_pair( strItem1, strItem2 ) );
++m_uCount;
}
在我的源代码中,第二个函数比第一个函数更重要!非常感谢您的任何帮助或建议。
const std::map<Count, const std::string>::const_iterator it = m_mCount.begin();
应移除第一个const
。否则,它将无法向前移动,也无法通过m_mCount
进行迭代。
相关文章:
- 在C++中搜索嵌套多映射值
- 映射如何在给定键处搜索值
- 在多重映射中搜索一系列键
- 如何搜索所有映射键 C++
- C++映射中搜索元组错误:无法将"int"左值绑定到"int&&"
- 使用迭代器搜索映射中的值
- 通过常量std::映射进行搜索
- 在C++中使用映射而不是数组来保护在数组边界之外的搜索
- 数组、矢量、映射和列表数据搜索
- 我可以将可搜索字符设备“/dev/mem_8”映射到内存并使用x86_64指令访问它吗?
- 在映射中搜索值的C++类和方法
- C++映射从迭代器位置开始搜索
- 在一对多映射中搜索一个密钥
- 映射和二叉搜索树
- 在大内存映射文件中搜索
- 在映射中搜索:成员与非成员lower_bound
- 在c++中搜索映射的元素
- 元素在多映射中可搜索吗?需要吗
- 如何在STL映射中搜索键
- 使用 STL 映射/集合/多重集/多重映射,如何查找大于或等于搜索键的第一个值