我在Java中有与此类似的代码,但是让它在C ++中运行却是另一回事。目前,此功能确实可以正确找到密钥,并将该密钥推到向量的背面。我想同时获取键及其值对,将其全部插入向量的相同索引中。我已经研究过将一对用于向量,所以它会是(键,一对值),但我不确定我能做得到。
vector<string> getIfPresentPartOfSpeech(multimap<string, pair<string, string>> dictionary, string name) {
vector<string> response;
auto it = dictionary.begin();
while (it != dictionary.end()) {
// Check if value of this entry matches with given value
if (it->second.first == name) {
// Yes found
// Push the key in given map
response.push_back(it->first);
}
// Go to next entry in map
it++;
}
return response;
}
答案 0 :(得分:2)
为您的response
定义一个结构,并返回该类型的std::vector
。
struct Response {
std::string key;
std::pair<std::string, std::string> resp_pair;
};
vector<Response> getIfPresentPartOfSpeech(multimap<string, pair<string, string>> dictionary, string name) {
vector<Response> response;
auto it = dictionary.begin();
while (it != dictionary.end()) {
// Check if value of this entry matches with given value
if (it->second.first == name) {
// Yes found
// Create a Response object
Response resp;
resp.key = it->first;
resp.resp_pair = it->second;
// Push it in the vector
response.push_back(resp);
}
// Go to next entry in map
it++;
}
return response;
}
还可以通过使用emplace_back
而不是push_back
来进行优化,以避免容器元素的多个副本通过。
答案 1 :(得分:1)
您可以在std::copy_if
中使用<algorithm>
,而不必手动编写显式循环。还要通过(常量)引用传递dictionary
以避免不必要的复制:
#include <algorithm>
#include <map>
#include <utility>
#include <vector>
// You'll need C++14 to use `auto const& kv` in your lambda expression
std::vector<std::pair<const std::string, std::pair<std::string, std::string>>>
getIfPresentPartOfSpeech(
const std::multimap<std::string, std::pair<std::string, std::string>>&
dictionary,
const std::string& name)
{
std::vector<
std::pair<const std::string, std::pair<std::string, std::string>>>
res;
std::copy_if(begin(dictionary), end(dictionary), std::back_inserter(res),
[&](auto const& kv) { return kv.second.first == name; });
return res;
}
//test
int main()
{
std::multimap<std::string, std::pair<std::string, std::string>> dictionary{
{"first", {"abc", "def"}},
{"second", {"123", "456"}},
{"first", {"asdf", "fdas"}},
{"thrid", {"abc", "123"}},
};
auto res = getIfPresentPartOfSpeech(dictionary, "abc");
for (auto& x : res)
std::cout << x.first << " " << x.second.first << " " << x.second.second
<< "\n";
}
使用类似@aep的结构建议会更清晰。您需要一个额外的转换构造函数才能与std::copy_if
一起使用:
struct Response {
std::string key;
std::pair<std::string, std::string> value;
// Provide a conversion constructor that convert your `dictionary` multimap
// key-value pair into a Response object
Response(const std::pair<const std::string,
std::pair<std::string, std::string>>& kv)
: key{kv.first}, value{kv.second}
{}
};
std::vector<Response> getIfPresentPartOfSpeech(
const std::multimap<std::string, std::pair<std::string, std::string>>&
dictionary,
const std::string& name)
{
std::vector<Response> res;
std::copy_if(begin(dictionary), end(dictionary), std::back_inserter(res),
[&](auto const& kv) { return kv.second.first == name; });
return res;
}