我对C ++还是很陌生。 A在GCC中使用std :: map。由于(我认为)很难读取GDB的std :: map输出。我想将地图导出到矢量。
现在我已经弄清楚了(此论坛提供了一些有用的信息):)如何将地图导出到矢量。现在我正确理解了c ++参考,std :: map不是线程安全的。所以我建立了一个Mutex arround std :: map
template<typename Mutex_Type_T, typename Key_Type_T, typename Mapped_Type_T>
class CConcurrent_Dictionary
{
public:
CConcurrent_Dictionary();
class CSingle_Element
{
public:
Key_Type_T key = { };
Mapped_Type_T mapped_type = { };
};
/**
* Exports all contect of std::map to a vector
* @param mutex_timeout Mutex timeout
* @return Vector with all contents of std::map
*/
std::vector<CSingle_Element> Export(const uint32_t mutex_timeout) const;
private:
const Mutex_Type_T mutex;
map<Key_Type_T, Mapped_Type_T> dictionary;
};
Export()的实现如下所示
template<typename Mutex_Type_T, typename Key_Type_T, typename Mapped_Type_T>
std::vector<CConcurrent_Dictionary<Mutex_Type_T, Key_Type_T, Mapped_Type_T>::CSingle_Element> CConcurrent_Dictionary<Mutex_Type_T, Key_Type_T, Mapped_Type_T>::Export(const uint32_t mutex_timeout) const
{
std::vector<CSingle_Element> res = {};
CLock_Guard_New<Mutex_Type_T> lock(mutex);
if(false == lock.Lock(mutex_timeout))
{
return res;
}
//export all Elements
for(const auto& single_element : dictionary)
{
CSingle_Element single = {};
single.key = single_element.first;
single.mapped_type = single_element.second;
res.push_back(single);
}
return res;
}
但是问题无法编译
note: expected a type, got 'NDictionary::CConcurrent_Dictionary<Mutex_Type_T, Key_Type_T, Mapped_Type_T>::CSingle_Element'
error: template argument 2 is invalid
error: prototype for 'int NDictionary::CConcurrent_Dictionary<Mutex_Type_T, Key_Type_T, Mapped_Type_T>::Export(uint32_t) const' does not match any in class 'NDictionary::CConcurrent_Dictionary<Mutex_Type_T, Key_Type_T, Mapped_Type_T>'
向量模板参数似乎不正确。
据我所知
CConcurrent_Dictionary :: CSingle_Element 成为std :: vector的唯一模板参数。那你能帮我吗?我想念什么?
答案 0 :(得分:1)
尝试以下操作:std::vector<typename CConcurrent_Dictionary<Mutex_Type_T, Key_Type_T, Mapped_Type_T>::CSingle_Element>