我正在尝试将一些值对插入到std :: map中.在第一种情况下,我收到一个指向地图的指针,取消引用它并使用下标运算符来赋值.即
(*foo)[index] = bar;
Run Code Online (Sandbox Code Playgroud)
稍后,当我尝试迭代集合时,我返回的键/值对在除了第一个(map.begin())项之外的所有情况下都包含null属性.奇怪的是,如果我通过map的insert函数进行插入,一切都很顺利,即:
foo->insert(std::pair<KeyType,ValueType>(myKey, myValue));
Run Code Online (Sandbox Code Playgroud)
为什么会这样?这两种方法在功能上是否相同?我已经在下面粘贴了一些实际代码的片段
...
typedef std::map<int, SCNode*> SCNodeMap;
...
void StemAndCycle::getCycleNodes(SCNodeMap* cycleNodes)
{
(*cycleNodes)[root->getId()] = root;
SCNode* tmp = root->getSucc();
while(tmp->getId() != root->getId())
{
// (*cycleNodes)[tmp->getId()] == tmp; // crashes (in loop below)
cycleNodes->insert(std::pair<int, SCNode*>(tmp->getId(), tmp));//OK
std::pair<int, SCNode*> it = *(cycleNodes->find(tmp->getId()));
tmp = tmp->getSucc();
}
// debugging; print ids of all the SCNode objects in the collection
std::map<int, SCNode*>::iterator it = cycleNodes->begin();
while(it != cycleNodes->end())
{
std::pair<int, SCNode*> p = (*it);
SCNode* tmp …Run Code Online (Sandbox Code Playgroud) 我有一个Visual Studio 2008 C++ 03应用程序,我想在其中创建一个std::map使用另一个迭代器std::map作为其键类型的迭代器.但是,当我尝试使用其键类型从该映射中擦除元素时,我遇到了问题.
在此示例中,当一个元素MyList变为超过5分钟时,计时器应该触发并将其从地图中删除并销毁其年龄计时器.
typedef std::map< Foo, FooBar > MyList;
typedef std::map< MyList::iterator, boost::shared_ptr< Timer > > MyListAgeTimers;
class A
{
public:
void AddItem( Foo& f, FooBar& fb )
{
CriticalSection::Guard g( lock_ );
std::pair< MyList::iterator, bool > new_foo =
my_list_.insert( std::make_pair( f, fb ) );
if( new_foo.second )
{
timers_.insert( std::make_pair(
new_foo.first,
boost::make_shared< Timer >( FiveMinutes, boost::bind( &A::OnAgeTimer, this, new_foo.first ) ) ) );
}
};
void OnAgeTimer( MyList::iterator item ) …Run Code Online (Sandbox Code Playgroud) 所以我有一个Node对象的抽象语法树.每个节点都有任意数量的子节点以及任意数量的标签,这些标签是通过std :: map结构附加到节点的信息的花絮.现在我想以类似XML的格式打印整个语法树.为此我使用这个功能:
int __ostreamNode_indent = 0;
std::ostream & operator << ( std::ostream & ss, Node* n )
{
for( int i = 0 ; i < __ostreamNode_indent ; ++i )
ss << " ";
ss << "<" << n->getSymbolType() << " ";
for( std::map<std::string,std::string>::iterator itr = n->getTags().begin() ; itr != n->getTags().end() ; ++itr )
{
ss << itr->first << "=\"" << itr->second << "\" ";
}
ss << "numtags=" << n->getTags().size() << " ";
if( n->getChildren().size() == 0 ) …Run Code Online (Sandbox Code Playgroud) 什么是将map的键集转换为键的向量的有效方法,目前我正在迭代我的地图并将iter.first添加到vector中,这类似于Java的KeySet api?
只是想优化一些std::map代码.地图包含通过字符串标识符访问的对象.
例:
std::map<std::string, CVeryImportantObject> theMap;
...
theMap["second"] = new CVeryImportantObject();
Run Code Online (Sandbox Code Playgroud)
现在,当使用find-function as时theMap->find("second"),会转换为String std::string("second"),这会导致新的字符串分配(在使用IDL = 2时使用Visual Studio).
1.是否有可能使用仅字符串类来避免此类分配?
我故意试图使用另一个String-Class:
std::map<CString, CVeryImportantObject> theMap;
Run Code Online (Sandbox Code Playgroud)
这段代码也有效.但CString 确实是一个对象.
并且:如果你从地图中删除一个对象,我需要释放相关的对象和密钥,是吗?
有什么建议?
有一些关于这个问题的帖子,但没有一个满足我.我没有openMp 3.0支持,我需要在地图上并行化迭代.我想知道这个解决方案是否有效:
auto element = myMap.begin();
#pragma omp parallel for shared(element)
for(int i = 0 ; i < myMap.size() ; ++i){
MyKeyObject * current_first = nullptr;
MyValueObject * current_second = nullptr;
#pragma omp critical
{
current_first = element->first;
current_second = element->second;
++element;
}
// Here I can use 'current' as in a usual loop
}
Run Code Online (Sandbox Code Playgroud)
所以我使用for循环只是为了确保线程将处理相同数量的地图元素.这是正确的猜测还是会失败?
ps:我正在开发visual studio 2012,所以如果你有关于如何让我的编译器支持openMp 3.0的提示,这也将解决我的问题..
所以我在我的程序中检查事实时遇到了问题:代码:向量包含3种类型的派生对象我只想要向量中每个基础对象的子总数.我似乎无法找到适当的语法.
class Base{
virtual void method() = 0;
}
class derived_1 : public Base{
virtual void method();
}
class derived_2 : public Base{
virtual void method();
}
class derived_3 : public Base{
virtual void method();
}
class general_class{
private:
//objects of derived types have been instantiated into the vector already
map<string,vector<Base*>> base_map;
void print(){
//This line prints the key and size
cout << iter->first << " " << iter->.size();
int d1_count = 0, d2_count = 0,d3_count = 0;
for(iter=accounts_map.begin();iter !=accounts_map.end();iter++){ …Run Code Online (Sandbox Code Playgroud) 请参阅此答案,以了解如何在不复制地图值的情况下将其插入到stdmap中。
继续回答-假设我的Foo类型如下所示:
struct Foo {
const int& intref_;
std::mutex mutex_;
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用聚合初始化来初始化
Foo{7}
Run Code Online (Sandbox Code Playgroud)
要么
Foo{7, std::mutex()}
Run Code Online (Sandbox Code Playgroud)
是否可以通过类型将其放置到地图中?
std::map<size_t, Foo> mymap;
Run Code Online (Sandbox Code Playgroud)
我知道我可以为此写一个构造函数,Foo但是可以用聚合初始化来代替吗?
链接到编译器资源管理器:
相关的c ++参考:
https://en.cppreference.com/w/cpp/container/map/try_emplace
https://en.cppreference.com/w/cpp/language/aggregate_initialization
我有这个问题:
我有一个std::map代表整数的字符串,代表水果列表:
map<string, int> fruits{
{"Apple", 5}, {"Grapefruit", 7}, {"Cherry", 10}, {"Grapes", 16}
};
for (const auto& p : fruits)
cout << p.first << " " << p.second << endl;
cout << endl << endl;
auto it = fruits.begin();
++++it;
using type = std::pair<class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char> > const, int>;
auto it2 = std::lower_bound(fruits.cbegin(), fruits.cend(), type{"Cherry", 10});
// auto it3 = std::lower_bound(fruits.cbegin(), fruits.cend(), pair<string, int>{ "Cherry", 10 });
auto it4 = std::lower_bound(fruits.cbegin(), fruits.cend(), pair<const string, int>{ …Run Code Online (Sandbox Code Playgroud) 我编码很有趣。我创建了一个地图矢量来查看我可以使用容器做什么。当我遍历矢量时,只有Alfred和Angela出现。如何显示所有名称?可能吗 这是我到目前为止的内容:
#include <map>
#include <iostream>
#include <conio.h>
#include <vector>
#include <string>
int main()
{
//create a map
std::map<std::string, unsigned int> mySuperCoolMap;
mySuperCoolMap["Edward"] = 39;
mySuperCoolMap["Daniel"] = 35;
mySuperCoolMap["Carlos"] = 67;
mySuperCoolMap["Bobby"] = 8;
mySuperCoolMap["Alfred"] = 23;
std::cout << "\n\n";
//Ranged based for loop to display the names and age
for (auto itr : mySuperCoolMap)
{
std::cout << itr.first << " is: " << itr.second << " years old.\n";
}
//create another map
std::map<std::string, …Run Code Online (Sandbox Code Playgroud)