我多次发现,当在类中将 std::map 声明为静态内联(C++ 17)时,
struct MyStruct
{
static inline std::map <A, B> mymap;
MyStruct(A& a, B& b)
{
mymap[a] = b;
}
};
Run Code Online (Sandbox Code Playgroud)
如果较早调用 MyStruct 构造函数,即在 main 之前、在第一次使用映射成员时调用,它将会崩溃。
如果 std::map 以不同的方式声明,即
struct MyStruct
{
static std::map <A, B>& mymap()
{
static std::map <A, B> map;
return map;
}
MyStruct(A& a, B& b)
{
mymap()[a] = b;
}
};
Run Code Online (Sandbox Code Playgroud)
那么就不会发生崩溃。
我本以为在这两种情况下,都会在允许继续调用 MyStruct 构造函数之前初始化映射。
谁能解释这里发生了什么?
我有std::vector<std::map<int, std::unique_ptr<int>>>容器(如果为了简化)。最初,我必须插入std::vector一定数量的std::map,每个都有一个键值对。我尝试过这样的代码:
#include <iostream>
#include <map>
#include <vector>
#include <memory>
using namespace std;
int main()
{
vector<map<int, std::unique_ptr<int>>> data{};
for (int x = 0; x < 10; x++)
{
data.emplace_back(std::make_pair(x, std::make_unique<int>(x)));
}
}
Run Code Online (Sandbox Code Playgroud)
但这没有用。我怎样才能改变它以使其按预期工作?
编辑:我明白我的错误,我必须使用这段代码来添加元素:
std::map<int, std::unique_ptr<int>> m;
m.emplace(x, std::make_unique<int>(y));
data.emplace_back(std::move(m));
Run Code Online (Sandbox Code Playgroud)
然后,该代码可以在在线编译器中运行,但在 Visual Studio 中仍然无法运行。
我有一个名为 foo 的结构体,还有一个名为 mp 的映射,但由于某种原因,当我插入 {5, 0, 3} 并查询 {5, 3, 0} 时,映射显示它包含 {5, 3 , 0} 当它实际上没有时:
#include <bits/stdc++.h>
using namespace std;
struct foo {
int v1, v2, v3;
friend bool operator<(const foo &a, const foo &b) {
return a.v1 < b.v1;
}
friend bool operator==(const foo &a, const foo &b) {
return (a.v1 == b.v1) && (a.v2 == b.v2) && (a.v3 == b.v3);
}
};
int main() {
map<foo, int> mp;
mp[{5, 0, 3}] = 1;
if(mp[{5, 3, …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <string>
#include <map>
#include <vector>
class base {};
class derived1 : public base
{
public:
unsigned short n;
derived1()
{
n = 2;
}
};
class derived2 : public base {};
void main()
{
// way 1
{
std::vector<derived1> a1;
std::vector<derived2> a2;
std::map<std::string, base*> b;
a1.push_back(derived1());
b["abc"] = &a1.at(0);
std::cout<<(dynamic_cast<derived1*>(b.find("abc")->second))->n<<std::endl;
}
// way 2
{
std::map<std::string, base*> b;
b["abc"] = new derived1();
std::cout<<dynamic_cast<derived1*>(b.find("abc")->second)->n<<std::endl;
delete dynamic_cast<derived1*>(b.find("abc")->second);
}
}
Run Code Online (Sandbox Code Playgroud)
错误是"'dynamic_cast':'base'不是多态类型".应该怎么做才能解决这个问题?一切都是在way1和way2中正确清理了吗?
我想要一个映射将字符串键映射到a std::function<T()>或std::function<T(int)>(但不是两个都给定键)的映射.我得到一个编译错误,似乎没有模板std::function<T(...)>.我希望能够使用lambdas作为值.
首先,以这种方式存储功能是否安全?其次,如果是这样,语法是什么?(当从地图中检索到函数时,函数是一元函数还是无效函数.)如果不是上述函数,那么什么是合理的替代方案?指针杀死了lambda的想法,对...
C++ 11没问题.
我在C++ 03环境中工作,并将一个函数应用于地图的每个键是很多代码:
const std::map<X,Y>::const_iterator end = m_map.end();
for (std::map<X,Y>::const_iterator element = m_map.begin(); element != end; ++element)
{
func( element->first );
}
Run Code Online (Sandbox Code Playgroud)
如果key_iterator存在,相同的代码可以利用std::for_each:
std::for_each( m_map.key_begin(), m_map.key_end(), &func );
Run Code Online (Sandbox Code Playgroud)
那为什么不提供呢?有没有办法让第一种模式适应第二种模式?
我创建了一个使用char和int映射的函数,这样当一个数字作为字符串传递时,我可以检查每个字符以确保它是一个int.然后我接受该字符串并将其转换为纯int.出于某种原因,当它检查'0'时,它会在找到时抛出错误.我知道有更好的方法可以做到这一点,我已经在我的大型计划中实施了许多方法.对于正在发生的事情,这是一个纯粹的学术问题.
main.cpp中
#include <iostream>
#include <string>
#include <map>
class Logic {
public:
Logic();
std::map <char, int> IsNumber;
bool CheckForInterger(std::string);
int StringToInt(std::string);
};
Logic::Logic() {
IsNumber = { { '0',0 },{ '1',1 },{ '2',2 },{ '3',3 },{ '4',4 },{ '5',5 },
{ '6',6 },{ '7',7 },{ '8',8 },{ '9',9 } };
}
bool Logic::CheckForInterger(std::string word) {
for (char character : word) {
if (IsNumber[character]) {
//do nothing and check next char
}
else {
std::cout << "\n\"" << character << "\" …Run Code Online (Sandbox Code Playgroud) 我试图为一些C++数据结构编写一个C包装器.现在我有以下内容foo.cpp
typedef std::map<unsigned int, void *> _Map;
extern "C"{
void* map_create()
{
return reinterpret_cast<void*> (new _Map);
}
void map_put(void *map, unsigned int k, void *v)
{
Map *m = reinterpret_cast<_Map *> (map);
m->insert(std::pair<unsigned int, void *>(k, v));
}
}
Run Code Online (Sandbox Code Playgroud)
在foo.h我有
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
typedef void *Map;
EXTERNC void *map_create();
EXTERNC void map_put(void* map, unsigned int k, int v);
Run Code Online (Sandbox Code Playgroud)
我包括foo.h,我很高兴去.
现在,我想迭代一张地图并注意到C++通过迭代器来做到这一点.我没有使用C++的经验,也不知道如何实现迭代器.
我可以std::map使用C包装器迭代吗?这些函数定义将如何呈现?我将如何在我的C代码中的for循环中使用它们?
我正在寻找一种方法来初始化std :: map中的第一个值,然后根据键初始化第二个值.这是我的代码:
#pragma once
#include <string>
#include <map>
class Student
{
public:
Student(
double Score_Maths,
double Score_Eng,
double Score_Chem,
double Score_Bio,
double Score_His
);
~Student();
private:
std::string Surname;
std::map<std::string, double> Subject_Scores = { {"Maths"}, {"English"}, {"Chemistry"}, {"Biology"}, {"History"} };
};
Run Code Online (Sandbox Code Playgroud)
我想要做的是,在类中已经有了这些键,然后使用构造函数初始化值,但当然在初始化地图时显示错误,任何帮助?
我使用std::unique_ptr自定义删除器作为a的值,std::map如下所示:
#include <iostream>
#include <memory>
#include <map>
void deleter(int* p){
std::cout<<"Deleting..."<<std::endl;
delete p;
}
int main()
{
std::map<char, std::unique_ptr<int, void(*)(int*)>> a;
std::unique_ptr<int, void(*)(int*)> p{new int{3}, deleter};
a['k'] = std::move(p);
}
Run Code Online (Sandbox Code Playgroud)
插入值时,我使用std::move,但不会编译.
我究竟做错了什么?
您会在链接后看到错误.
c++ ×10
stdmap ×10
iterator ×2
c ×1
c++03 ×1
c++11 ×1
class ×1
containers ×1
logic ×1
map ×1
polymorphism ×1
std-function ×1
stdmove ×1
stdvector ×1
types ×1
unique-ptr ×1