C++ '的赋值中的不兼容类型;std::string{aka std::basic_string<;char>;}';到 intmain() { std::string my_string=“16657”; 标准::地图m_msg_int; std::字符串arrId[2]; arrId[0]=“ABC”; arrId[1]=“XYZ/CDE”; m_msg_int[my_string]=arrId[2]; 标准::cout

C++ '的赋值中的不兼容类型;std::string{aka std::basic_string<;char>;}';到 intmain() { std::string my_string=“16657”; 标准::地图m_msg_int; std::字符串arrId[2]; arrId[0]=“ABC”; arrId[1]=“XYZ/CDE”; m_msg_int[my_string]=arrId[2]; 标准::cout,c++,arrays,maps,associative,C++,Arrays,Maps,Associative,一致 int main() { std::string my_string= "16657"; std::map<std::string, std::string[2]> m_msg_int; std::string arrId[2]; arrId[0] = "ABC"; arrId[1] = "XYZ/CDE"; m_msg_int[my_string] = arrId[2]; std::cout<<"MSGID"<<msgId[0]<<end

一致

int main()
{

std::string my_string= "16657";
std::map<std::string, std::string[2]> m_msg_int;
std::string arrId[2];
arrId[0] = "ABC";
arrId[1] = "XYZ/CDE";
m_msg_int[my_string] = arrId[2];
std::cout<<"MSGID"<<msgId[0]<<endl;

 }
您正在访问数组
arrId
越界,因为它的大小为2,读取
arrId[2]
会导致未定义的行为。即使您不想访问越界

m_msg_int[my_string] = arrId[2];
是对字符串数组的引用,并且

m_msg_int[my_string]
是字符串。可能您正在尝试分配

arrId[2]
<>但是在C++中,不能将基本数组分配给基本数组。
m_msg_int[my_string] = arrId;
#包括
#包括
#包括
int main()
{
std::string my_string=“16657”;
标准::地图m_msg_int;
std::数组arrId;
arrId[0]=“ABC”;
arrId[1]=“XYZ/CDE”;
m_msg_int[my_string]=arrId;

//std::cout将
std::string
分配给
std::string
的数组

#include <array>
#include <map>
#include <string>

int main()
{
    std::string my_string= "16657";
    std::map<std::string, std::array<std::string, 2>> m_msg_int;
    std::array<std::string, 2> arrId;
    arrId[0] = "ABC";
    arrId[1] = "XYZ/CDE";
    m_msg_int[my_string] = arrId;
    //std::cout<<"MSGID"<<msgId[0]<<endl;
}
现在,我们将
std::string
数组分配给映射

注意:数组索引范围为
n
n-1
。您不能执行此操作:

m_msg_int[my_string] = arrId;

因为
arrId
有2个元素,但index
2
表示第三个元素。

是的,
m\u msg\u int[my\u string]
是两个字符串的数组,而
arrId[2]如果它已经存在,它将是一个字符串。您需要一种不同的方法,并且需要在您最喜欢的C++书籍中学习更多关于数组的内容。<代码> MyMSGItI[MyStry] = AdRID;< /C> >不允许。
m_msg_int[my_string] = arrId[2];