【发布时间】:2019-11-11 16:58:10
【问题描述】:
我想为下面的练习做一个操作,我试图将一组字符串中的一个元素添加到一组字符串的向量中。
#include <iostream>
#include <set>
#include <vector>
int main() {
std::set<std::string> remaining_cards = {"2S", "3S", "4S", "5S", "6S"};
std::vector<std::set<const std::string>> player_cards;
int random_number;
random_number=2;
auto it = remaining_cards.cbegin();
std::advance(it, random_number);
std::cout << *it << std::endl;
// add one element to a vector of sets of strings from set of strings
player_cards.emplace_back(*it);
// remove that element from the original set
remaining_cards.erase(it);
return 0;
}
我有以下问题:
- 为什么我收到错误
::const_reference = const std::basic_string<char>&]' cannot be overloaded. - 我试图从
player_cards声明中删除const,但后来我得到no matching function for call to 'std::set<std::basic_string<char> >::set(const std::basic_string<char>&) - 当我执行
emplace_back(*it);时,是否必须取消引用it? - 我怀疑我的问题是因为我尝试将集合中的一个元素放入字符串集合的向量中。我是否需要首先创建迭代器指向的元素的集合?我假设它只指向一个字符串。
- emplace_back 没有直接创建集合的能力吗?
- 我可以做类似
emplace_back(std::set(*it));的事情吗?
以上所有内容可能在概念上都是错误的,因为我是 C++ 新手,但我试图了解将元素从集合添加到集合向量的最佳方法是什么。它似乎比预期的要复杂。
【问题讨论】:
-
std::set<const std::string>-- Try and add a string to that container.