C++ C++;重构阵列

C++ C++;重构阵列,c++,arrays,C++,Arrays,有人能帮我吗?为您的时间定义一个struct或class来表示房间数据,并使用std::vector、std::map或std::无序映射来存储房间: #include <iostream> #include <vector> #include <map> struct Room { std::string name; Room(std::string _name) : name(_name) {} Room() {} }; int ma

有人能帮我吗?为您的时间定义一个
struct
class
来表示房间数据,并使用
std::vector
std::map
std::无序映射
来存储房间:

#include <iostream>
#include <vector>
#include <map>

struct Room {
   std::string name;
   Room(std::string _name) : name(_name) {}
   Room() {}
};

int main() {
    std::vector<Room> rooms{{"Room1"}, {"Room2"}};
    std::cout << rooms[0].name << std::endl; // prints "Room1"

    std::map<int, Room> roomsMap{
        {1, Room{"Room1"}},
        {2, Room{"Room2"}}
    };
    std::cout << roomsMap[1].name << std::endl; // prints "Room1"
    return 0;
}
#包括
#包括
#包括
结构室{
std::字符串名;
房间(std::string _name):名称(_name){}
房间(){}
};
int main(){
向量室{{“Room1”}、{“Room2”};

std::cout定义一个
struct
class
来表示房间数据,并使用
std::vector
std::map
std::无序映射
来存储房间:

#include <iostream>
#include <vector>
#include <map>

struct Room {
   std::string name;
   Room(std::string _name) : name(_name) {}
   Room() {}
};

int main() {
    std::vector<Room> rooms{{"Room1"}, {"Room2"}};
    std::cout << rooms[0].name << std::endl; // prints "Room1"

    std::map<int, Room> roomsMap{
        {1, Room{"Room1"}},
        {2, Room{"Room2"}}
    };
    std::cout << roomsMap[1].name << std::endl; // prints "Room1"
    return 0;
}
#包括
#包括
#包括
结构室{
std::字符串名;
房间(std::string _name):名称(_name){}
房间(){}
};
int main(){
向量室{{“Room1”}、{“Room2”};

std::难道你要的是字典(map),而不是列表吗?比如
std::vector
?看看下面的结构数组,尽管你可能希望将它们存储在比数组更好的容器中:集合、列表或向量,或者索引类型为map更合适。你要的是字典(map),而不是列表。如
std::vector
?请查看示例中的结构数组,尽管您可能希望将它们存储在比数组更好的容器中:集合、列表或向量或索引类型(如映射)更合适。