有人可以提供一个在c ++中返回map的函数的实际示例。
我尝试过其他帖子的答案,但我不知道如何申请。
这是我的工作代码:
auto DataArray = jvalue.at(U("data")).as_array();
//Make an associative array or map with key value pair from extracted json data
std::map<int, std::string> staffMap;
// loop through 'data' object
for (int i = 0; i < DataArray.size(); i++)
{
try
{
auto data = DataArray[i];
auto dataObj = data.as_object();
int key;
std::string value;
// loop through each object of 'data'
for (auto iterInner = dataObj.cbegin(); iterInner != dataObj.cend(); ++iterInner)
{
auto &propertyName = iterInner->first;
auto &propertyValue = iterInner->second;
//std::wcout << "Property: " << propertyName << ", Value: " << propertyValue << std::endl;
if (propertyName == L"_id")
{
key = propertyValue.as_integer();
}
else if (propertyName == L"name")
{
value = conversions::to_utf8string(propertyValue.as_string());
}
}
staffMap.insert(std::make_pair(key, value));
}
catch (const std::exception& e)
{
std::wcout << e.what() << std::endl;
}
}
// Iterate through map and display in terminal
std::map<int, std::string>::iterator iter;
std::wcout << "The list of staffs" << std::endl;
for (iter = staffMap.begin(); iter != staffMap.end(); iter++)
std::cout << iter->first << " " << iter->second << " ,";
我想要一个功能:
std::map<int, std::string> staffMap;
std::map<> GetStaffMap()
{
return staffMap;
}
// Give staffMap a data here
我找不到足够的教程来制作一个在c ++中返回std :: map的函数。希望有人能在这里帮助我。谢谢。
答案 0 :(得分:3)
我找不到足够的教程来创建一个在c ++中返回std :: map的函数。希望有人能帮助我
您需要指定确切类型string.words_and_punctuation
=> ["The", "man's", "hat", ",", "was", "very", "nice", "."]
string.words_and_punctuation.select(&:punctuation?)
=> ["man's", ",", "."]
:
std::map<int, std::string>
如果您能够使用C ++ 14,请使用auto
替代:
std::map<int, std::string> GetStaffMap()
{
return staffMap;
}
答案 1 :(得分:1)
下面的示例显示了如何在C ++中创建一个返回地图的函数。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="border.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.10/css/all.css" integrity="sha384-+d0P83n9kaQMCwj8F4RJB66tzIwOKmrdb46+porD/OvrJ+37WqIM7UoBtwHO6Nlg" crossorigin="anonymous">
</head>
<body>
<button class="btn"><i class=" fas fa-shipping-fast fa-inverse"></i></button>
<button class="btn"><i class=" fas fa-address-card fa-inverse"></i></button>
<button class="btn"><i class=" fas fa-camera-retro fa-inverse"></i></button>
<button class="btn"><i class="fas fa-flag fa-inverse"></i></button>
<button class="btn"></button>
<script type="text/javascript">
var buttons = document.getElementsByClassName("btn");
for (var i = 0; i < buttons.length; i++) {
document.getElementsByClassName("btn")[i].addEventListener("click", function() {
if(this.classList.contains("skill_lvl_3"))
this.classList.add("skill_lvl_4");
else if(this.classList.contains("skill_lvl_2"))
this.classList.add("skill_lvl_3");
else if(this.classList.contains("skill_lvl_1"))
this.classList.add("skill_lvl_2");
else if(this.classList.contains("skill_unlocked"))
this.classList.add("skill_lvl_1");
else
this.classList.add("skill_unlocked");
});
}
</script>
</body>
</html>