Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function that returns map in c++

Can someone please give an actual example of function that returns map in c++.

I tried answers from other posts but I don't know how to apply in my case.

This is my working code:

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 << " ,";

Let say I want a function:

std::map<int, std::string> staffMap;
std::map<> GetStaffMap()
{
  return staffMap;
}

// Give staffMap a data here

I can't find enough tutorial for making a function that returns std::map in c++. Hope someone could help me here. Thank you.

like image 229
noyruto88 Avatar asked May 03 '18 02:05

noyruto88


2 Answers

I can't find enough tutorial for making a function that returns std::map in c++. Hope someone could help me here

You need to specify the exact type, std::map<int, std::string>:

std::map<int, std::string> GetStaffMap()
{
    return staffMap;
}

If you are able to use C++14, use auto as an alternative:

auto GetStaffMap()
{
    return staffMap;
}
like image 122
Dean Seo Avatar answered Sep 28 '22 12:09

Dean Seo


The example below shows how to create a function in C++ that returns a map.

// Example program with a function returning a map
#include <iostream>
#include <string>
#include <map>

std::map<std::string, int> 
function()
{
    std::map<std::string, int> out;
    out["one"] = 1;
    out["two"] = 2;
    return out;
}


int main()
{
  std::map<std::string, int> out = function(); 
  for (const auto & iter : out)
    std::cout << iter.first << " = " << iter.second << std::endl;
}
like image 24
slaughter98 Avatar answered Sep 28 '22 11:09

slaughter98