pre*_*eys 0 c++ assignment-operator
例如,如果我想进行作业
std::vector<int> a;
std::vector<std::pair<std::string, int>> b;
a = b; // a takes all the second components of the pairs in b
Run Code Online (Sandbox Code Playgroud)
这样的自定义分配可能吗?
您似乎想要的是将一个容器转换为另一个容器。这通常是通过适当命名的std::transform标准函数来完成的:
std::vector<int> a(b.size()); // Set as same size as the source vector
std::transform(begin(b), end(b), begin(a), [](auto const& pair)
{
return pair.second; // Use the second value of each pair from the source
});
Run Code Online (Sandbox Code Playgroud)