c++ - 使用构造函数向 std::set 插入一个元素

标签 c++ stl

是否可以向 std::set 插入一个新元素,例如 std::list 的情况:

//插入一个名为“string”的元素到mylist的子列表

std::list< std::list<string> > mylist;
mylist.push_back(std::list<string>(1, "string"));

现在,mylist 在它的 std::list 类型的子列表中有一个 std::string 类型的元素。

如果 std::set 是 std::list 的子集我的列表即

std::list<std::set <string>> mylist;

如果你不能,那为什么不呢?

最佳答案

我认为这应该可以解决问题:

int main()
{
    string s = "test";
    set<string> mySet(&s, &s+1);

    cout << mySet.size() << " " << *mySet.begin();

    return 0;
}

有关将 &s 视为数组的合法性和有效性的说明,请参阅此讨论:string s; &s+1; Legal? UB?

关于c++ - 使用构造函数向 std::set 插入一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2404781/

相关文章:

c++ - GetFileAttributeW 对非 ASCII 字符失败

c++ - 对类构造函数的 undefined reference

c++ - 模糊图像的卷积产生一个粗略的输出图像

c++ - 如何在 C++\wxWidgets 中下载文件

c++ - 最有可能使用什么方法来处理多个键以使用 STL 容器获取值?

c++ - 在 C++ STL 中通过邻接表正确实现图

c++ - 在 C++ 中获取 std::map 的最大键的时间复杂度是多少?

c++ - VC++ 2010 "array is not a member of std"错误

c++ - 遗留 c++ 代码不包含 std::prefix

c++ - std::unordered_set<Foo> 作为类 Foo 的成员