标签: stdmap

C++ - 直接插入 std::map 而不使用赋值运算符

std::map想在其中插入一个对象,但我想避免不必要的复制,因为它是一个大对象,并且复制需要时间。我试过这个:

MyPODType data(otherData);
data.modify();
myMap["Some key"] = data;
Run Code Online (Sandbox Code Playgroud)

然而,这需要 2 个副本:一份用于复制otherData,一份在赋值运算符中用于插入对象。由于是POD,复制构造函数和赋值运算符都是默认的。

我怎样才能只用一份副本来做到这一点?我意识到一些编译器会优化第二个副本,但我想以可移植的方式做到这一点。

c++ stdmap copy-constructor assignment-operator c++11

2
推荐指数
1
解决办法
1258
查看次数

迭代 std::array 中的记录

我正在创建一个 2D std::array,并且想要按记录(而不是按索引)迭代 2D std::array 记录

    std::array<std::array<int, 2>, 6> m_setSockarr;
    m_setSockarr = {{{0,1},{1,2},{2,3},{4,5},
                        {5,6},{6,7}}};
    for(auto i=m_setSockarr.begin();i !=m_setSockarr.end();i++)
    {
            //code here to print record wise.
    }
Run Code Online (Sandbox Code Playgroud)

我想以记录方式打印值,而不是以索引方式打印值(不使用另一个循环并最终打印 m_setScokarr[i][j])。

在 std::Map 中,我们可以使用 it->first 和 it->second 逐条打印记录(Map 是关联容器)

尝试以不同的方式打印,例如新的索引增量等,但没有进展。

我想要的输出类似于在一个 for 循环中使用 std::array 的迭代器

0,1
1,2
2,3
4,5
5,6
6,7
Run Code Online (Sandbox Code Playgroud)

想知道我们是否可以使用 std::array(SequenceContainer) 来打印记录,类似于我们使用 std::map 。

for-loop stdmap c++11 stdarray

2
推荐指数
1
解决办法
1万
查看次数

模板类型参数的模板实参必须是类型

我正在开发一个template Subject class将用作某些事件驱动编程的基类。目前,我正在进行回调注册,并且我Template argument for template type parameter must be a type; did you forget 'typename'?在 行收到了“” std::map

我觉得答案与此链接相关,但我无法形成它来解决我的问题。

这是类的标题Subject

#ifndef Subject_hpp
#define Subject_hpp

#include "Observer.hpp"

#include <list>
#include <map>

template<class T>
class Subject
{
public:
    Subject();
    virtual ~Subject();

    virtual void attach( Observer* const object, void(Observer::* const func)(T) );
    virtual void detach(const int pObserverId);
    virtual void notify(T& pEvent);

    // Clear the subject: Clear the attached observer list
    void clear();

private:


    std::list< …
Run Code Online (Sandbox Code Playgroud)

c++ templates stdmap

2
推荐指数
1
解决办法
1万
查看次数

使用自定义的 less 运算符实现迭代 std::map 会减少它包含的元素

假设我有以下简单程序(http://cpp.sh/5sygh):

#include <map>
#include <iostream>

using Key = std::pair<unsigned long, unsigned long long>;

struct KeyLess {
  bool operator()(const Key& lhs, const Key& rhs) {
      if (lhs.first < rhs.first) {
        return  true;
      }

      if (lhs.second < rhs.second) {
        return true;
      }

      return false;
  }
};

int main() {
    std::map< Key , int, KeyLess> m;
    m[Key{2, 169}] = 1;
    m[Key{1, 255}] = 2;
    m[Key{1, 391}] = 3;
    m[Key{1, 475}] = 4;

    std::cout << "Elements in map: " << m.size() << …
Run Code Online (Sandbox Code Playgroud)

c++ dictionary stdmap c++11 ranged-loops

2
推荐指数
3
解决办法
288
查看次数

在映射中插入一对时,C++ 是否需要额外的代码?

你好伟大的 StackOverflow 社区!我正在制作一些 C++,但在std::map.

这是 2 个映射,将类似 ID 存储unsigned int为键,将另一个对象存储为值:

    std::map<unsigned int, FIFO> _fifos;
    std::map<unsigned int, Kitchen> _kitchens;
Run Code Online (Sandbox Code Playgroud)

两个地图都private在一个类中,我在该类的public方法中插入这样的:

    FIFO newFIFO(_internalCount);
    Kitchen newKitchen(_args, newFIFO);

    _kitchens.insert(std::make_pair(_internalCount, newKitchen));
    _fifos.insert(std::make_pair(_internalCount, newFIFO));
Run Code Online (Sandbox Code Playgroud)

这里开始麻烦。
我的编辑器(VSCode)和编译器(g++)似乎都接受_fifos.insert()但不接受_kitchens.insert().

VSCode 说:

no instance of overloaded function "std::map<_Key, _Tp, _Compare, _Alloc>::insert [with _Key=unsigned int, _Tp=Kitchen, _Compare=std::less<unsigned int>, _Alloc=std::allocator<std::pair<const unsigned int, Kitchen>>]" matches the argument list..."
Run Code Online (Sandbox Code Playgroud)

虽然 g++ 首先显示了这一点,但在列出了 C++ 深处的一堆错误之后(特别是在 stl_pair.h 中):

error: no matching function for call …
Run Code Online (Sandbox Code Playgroud)

c++ stl stdmap std-pair

2
推荐指数
1
解决办法
131
查看次数

为什么即使在调用参数化构造函数时也会调用默认构造函数?

我有一个类,我正在使用参数化构造函数创建它的一个对象。在此期间,参数化构造函数和默认构造函数都已被调用。

这是我的片段:

class student {
    string name;
    int age;
public:
    student() {
        cout << "Calling the default constructor\n";
    }
    student(string name1, int age1) {
        cout << "Calling the parameterized const\n";
        name = name1;
        age = age1;
    }
    void print() {
        cout << " name : " << name << " age : " << age << endl;
    }
};

int main()
{
    map<int, student> students;
    students[0] = student("bob", 25);
    students[1] = student("raven", 30);

    for (map<int, student>::iterator it = students.begin(); it …
Run Code Online (Sandbox Code Playgroud)

c++ class stdmap default-constructor parameterized-constructor

2
推荐指数
1
解决办法
223
查看次数

将重物插入 std::map

VeryHeavy(args...)这段代码中的复制了多少次?

map.emplace(std::pair<VeryHeavyKey, VeryHeavy>(key, VeryHeavy(args...)));
Run Code Online (Sandbox Code Playgroud)

或者,也许,有更好的使用std::make_pair?复制对象是否有任何标准化的保证?在不复制的情况下将重物插入 std::map 的正确方法是什么?

c++ performance containers stdmap c++-standard-library

2
推荐指数
1
解决办法
77
查看次数

map.find() 返回看似随机的 map.end()

我在 C++ 中使用自定义比较器map。不幸的是,map.find()通常不会在地图中找到所需的条目。重现这一点的代码非常简单:

#include <array>
#include <iostream>
#include <map>

using namespace std;

typedef struct DATA_t {
  array<int, 4> data;
} DATA_t;

struct DATA_cmp {
  bool operator()(const DATA_t& a, const DATA_t& b) const {
    for (int i = 0; i < a.data.size(); ++i)
      if (a.data[i] < b.data[i]) return true;
    return false;
  }
};

int main(int argc, char** argv) {
  map<DATA_t, int, DATA_cmp> index;
  DATA_t data1 = {1, 2, 4, 8};
  DATA_t data2 = {1, 3, 5, 7}; …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap find comparator

2
推荐指数
1
解决办法
52
查看次数

c++ map 将其分配给 end() 后,它仍然执行一次迭代

我有以下代码,

#include <map>
#include <iostream>
int main() {
    std::map<int, int> test_m;
    test_m[1] = 1;
    test_m[2] = 2;
    test_m[3] = 3;
    test_m[4] = 4;
    test_m[7] = 7;
    test_m[8] = 8;
    auto it2 = test_m.find(2);
    auto it7 =test_m.find(7);
    auto it_end = test_m.end();
    for (auto it = it2; it!=test_m.end(); ++it) {
        std::cout << it->second << std::endl;
        if (it->second == 3) {
            it = test_m.end();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我期待:

2
3
Run Code Online (Sandbox Code Playgroud)

但相反我得到

2
3
8
Run Code Online (Sandbox Code Playgroud)

我可以知道为什么吗?

c++ stdmap

2
推荐指数
1
解决办法
46
查看次数

如何让map.emplace在c++17中自动选择构造函数?

首先在 c++17 中,我有一个类似的结构

typedef struct ST_A{
    int a;
    string s;

    ST_A(const int a, const string&s) :a(a),s(s) {}
}st_A;
Run Code Online (Sandbox Code Playgroud)

现在我有一个简单的地图,并根据 cppreference放置了两次

map<string, st_A> m1;
m1.emplace("c", 10, "c");                   ---> case 1
m1.emplace(std::piecewise_construct,        ---> case 2
        std::forward_as_tuple("c"),
        std::forward_as_tuple(10, "c"));
Run Code Online (Sandbox Code Playgroud)

情况 2 工作正常,但情况 1 编译错误。那么情况1如何解决并emplace尽可能简洁呢?

如果情况map更复杂怎么办?比如 map<string, deque<pair<int, st_A>>> m2,如何尽可能emplace简洁?

如果自定义结构体有更多构造函数怎么办?比如

typedef struct ST_A{
    int a = 0;
    string s;

    ST_A(const int a) :a(a) {}
    ST_A(const string&s) :s(s) {}
    ST_A(const int a, const string&s) :a(a),s(s) {}
}st_A; …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap emplace c++17

2
推荐指数
1
解决办法
142
查看次数