标签: stdmap

如何解决比较结构作为键映射c ++

没有主题解决我在 C++ 中比较结构作为键映射的问题。

结构体代码如下:

struct XYZ{
  int x, y, z;
}

struct XYZComp{
  bool operator()(const XYZ& l, const XYZ& r)
  {
    return ((l.x==r.x)&&(l.y==r.y)&&(l.z==r.z));
  }
}
Run Code Online (Sandbox Code Playgroud)

主要看起来像

int main()
{
  map<XYZ, int, XYZComp> m;
  m.insert(std::make_pair<XYZ,int>({1,2,3}, 1)); //ok

  map<XYZ, int, XYZComp>::iterator it = m.find({1,0,3});
  if(it!=m.end())
  {
    std::cout<<"Key exists in map"; 
  }
  else
  {
    m.insert(std::make_pair<XYZ,int>({1,0,3}, 1));
    //never come here 
    //compiler thinks key already exists in map
  }

return 0;
} 
Run Code Online (Sandbox Code Playgroud)

我只是在没有 XYZComparer 的情况下尝试过,但它仍然不起作用。

struct XYZ{
  int x,y,z;
  bool operator==(const XYZ& xyz)
  {
    return …
Run Code Online (Sandbox Code Playgroud)

c++ dictionary key stdmap

0
推荐指数
1
解决办法
5184
查看次数

自定义类 std::map 奇怪的行为

使用我创建的自定义类作为键在地图中搜索键时,我遇到了奇怪的行为。

尽管它们存在于地图中,但它似乎没有找到密钥。

有谁知道这是什么原因?

代码(可以在这里运行):

#include <iostream>
#include <map>

using namespace std;

typedef short int dimension;

class Point {
public:
    Point() : m_x(0), m_y(0) {}

    Point(const Point &other) : m_x(other.m_x), m_y(other.m_y) {};

    Point(dimension x, dimension y) : m_x(x), m_y(y) {}

    bool operator<(const Point &other) const {
        if (m_x < other.m_x) return true;
        return (m_y < other.m_y);
    }

private:
    dimension m_x;
    dimension m_y;
};


int main() {
    map<Point, bool> points = {{Point(0, 2), true},
                               {Point(1, 1), true},
                               {Point(2, 4), true}}; …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap std

0
推荐指数
1
解决办法
58
查看次数

插入 std::map 时强制初始化为 0

我有一个std::map<std::string,std::size_t>键映射计数器。当我增加一个计数器时,我不知道它是否已经存在。如果是,则递增。如果不是,则设置为 1。这很容易做到:

std::map<std::string,std::size_t> counters;
//...
auto c_it = counters.find(key);
if(c_it == counters.end())
  counters.insert(key,1);
else
  (*c_it)++;
Run Code Online (Sandbox Code Playgroud)

这是一个简单的事情的很多代码......我想这样做:

counters[key]++;
Run Code Online (Sandbox Code Playgroud)

但这会产生未定义的行为,因为std::size_t()在映射中不存在计数器时调用,并且不能保证std::size_t()初始化为 0。

我看到了两个潜在的解决方案:

  1. 查找/创建类似于std::size_t使用默认构造函数创建时强制初始化为 0的类型。std或 中有这样的类型boost吗?
  2. 查找/创建一个专用分配器来替换std::allocator<std::pair<const Key,T>>作为std::map. 但我不知道该怎么做。

注意:我只使用 C++11(我不想要 C++>=14 的解决方案)

c++ stdmap size-t allocator c++11

0
推荐指数
2
解决办法
82
查看次数

如何使用 C++ 中的键从地图中获取对

我有以下地图:

std::map<char, std::pair<int, int> > robots;
Run Code Online (Sandbox Code Playgroud)

如果输入满足某些条件,我将使用此函数来填充地图:

bool World::addRobot(int row, int col, char robot_name) {

    // This if block checks if the desired location is a valid 1 and keeps a track of all the robots already in the grid
    if (map_[row][col] == '1' && robots.find(robot_name) == robots.end()){
        map_[row][col] = robot_name;
        robots.insert(make_pair(robot_name, std::make_pair(row, col)));
    }
    else{std::cout << "Invalid input" << std::endl;}

  return true;
}
Run Code Online (Sandbox Code Playgroud)

每个机器人名称(只是一个字符)都与其位置(行/列坐标)一起保存。在以下函数中,我希望能够检索给定机器人名称的位置对:

std::pair<int, int> World::getRobot(char robot_name) {

    std::pair<int, int> location = robots.find(robot_name);
    return location;
}
Run Code Online (Sandbox Code Playgroud)

但名称location …

c++ stl stdmap std-pair

0
推荐指数
1
解决办法
836
查看次数

C++:wchar_t 不能作为键或值存储在 std::map 中

std::map<char, wchar_t>我正在尝试使用C++ 中的数据类型创建一个变量。当我尝试此操作时,Visual Studio 给出以下构建错误:

\n
\n

C2440“初始化”:无法从“初始化列表”转换为“std::map<char,wchar_t,std::less<char>,std::allocator<std::pair<const char,wchar_t>>>”

\n
\n

wchar_t当是键数据类型和char是值数据类型时也会发生同样的错误。

\n

您可以通过运行以下代码来复制此错误:

\n
const std::map<char, wchar_t> UNICODE_MAP = {\n    { 'X', L"\xe2\x96\x88" },\n    { 'G', L"\xe2\x96\x93" },\n    { 'O', L"\xe1\x97\xa3" },\n    { 'P', L"\xe1\x97\xa4" }\n};\n
Run Code Online (Sandbox Code Playgroud)\n

如何制作一个以键为 a char、以值作为wchar_t数据类型的映射?

\n

c++ wchar-t stdmap char

0
推荐指数
1
解决办法
307
查看次数

除非存在类对象的“空”(无参数)构造函数,否则将对象添加到 std::map 不起作用

我正在研究一个开源应用程序的代码。我制作了这段代码的一个更简单的版本,以隔离困扰我的问题(尽管我对这段代码有几个问题,我希望 C++ 专家能够帮助我,我将从主要问题开始)。

主要问题:为什么我需要一个“空”构造函数(无参数)来分配给 std::map 的对象类?

主要思想(参见下面的代码)是将 Variant 类的实例分配给 a std::map(其键是 a std::string)。这是代码:

#include <iostream>
#include <map>
#include <string>
#include <memory>

struct Data
{
public:
    Data(const void* data, size_t bytes) : bytes(bytes)
    {
        ptr = malloc(bytes);
        memcpy(ptr, data, bytes);
        std::cout << "in ctor of Data" << std::endl;
    }
    ~Data() { free(ptr); std::cout << "in dtor of Data" << std::endl; }
    void* ptr{ nullptr };
    size_t bytes;
};

struct DataStream 
{
public:
    DataStream(const std::shared_ptr<Data>& ptr, size_t size) : ptr(ptr), …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap stdmove

0
推荐指数
1
解决办法
328
查看次数

C++ 构造一个具有两个范围的映射

有没有办法在 C++ 中构造具有两个范围的映射?也就是说,不是调用默认构造函数并随后将元素插入到映射中:

for (size_t i = 0; i < Vector_1.size() && i < Vector_2.size(); i++) {
    mymap[Vector_1[i]] = Vector_2[i];
}
Run Code Online (Sandbox Code Playgroud)

我想通过调用某种构造函数来构建地图,该构造函数将两个范围作为参数,如下所示:

std::map<t1, t2> mymap(Vector_1.begin(), Vector_1.end(), Vector_2.begin(), Vector_2.end());
Run Code Online (Sandbox Code Playgroud)

或者:

mymap.insert(Vector_1.begin(), Vector_1.end(), Vector_2.begin(), Vector_2.end());
Run Code Online (Sandbox Code Playgroud)

我还没有找到,但也许还有办法做到这一点。是否有快捷方式从两个范围初始化地图,或者至少将两个范围插入地图?

c++ stdmap

0
推荐指数
1
解决办法
284
查看次数

编译器如何将值写入此构造中该对的第二个值?

map<int,int> a;
pair<std::map<int,int>::iterator ,bool> f;
f=(a.insert({0,0}));
cout<<f.second;
Run Code Online (Sandbox Code Playgroud)

为什么输出1呢?

对于该对中的任何值,它始终输出 1

c++ stdmap std-pair

0
推荐指数
1
解决办法
95
查看次数

额外的 std::map::contains 调用与处理异常?

c++中什么效率更高?

if (my_map.contains(my_key)) return my_map.at(my_key);
Run Code Online (Sandbox Code Playgroud)

或者

try { return my_map.at(my_key); } catch (std::out_of_range e) { ... }
Run Code Online (Sandbox Code Playgroud)

c++ stl stdmap c++20

0
推荐指数
1
解决办法
72
查看次数

为 gcc 编译器设置 std::map.end() 的哨兵值

这里我特别只关心GCC编译器和运行时代码的效率。

考虑下面的代码试试我

#include <iostream>
#include <map>

char Find(const std::map<int, char>& map, int key) {
    auto iter = map.find(key);
    if (iter == map.end()) 
        return 'X';
    return iter->second;
}

char Find2(const std::map<int, char>& map, int key) {
    return map.find(key)->second;
}

int main()
{
    // part 1
    std::map<int, char> x{{0,'0'}, {4,'4'}};
    std::cout << Find(x, 3) << std::endl;
    std::cout << Find(x, 4) << std::endl;
    std::cout << (int)Find2(x, 3) << std::endl; // returns 0
    std::cout << Find2(x, 4) << std::endl;

    // part 2: Find2 …
Run Code Online (Sandbox Code Playgroud)

c++ stdmap

0
推荐指数
1
解决办法
226
查看次数

标签 统计

c++ ×10

stdmap ×10

std-pair ×2

stl ×2

allocator ×1

c++11 ×1

c++20 ×1

char ×1

dictionary ×1

key ×1

size-t ×1

std ×1

stdmove ×1

wchar-t ×1