3 votes

Fonction qui renvoie une carte en c++

Quelqu'un peut-il donner un exemple concret d'une fonction qui renvoie une carte en c++.

J'ai essayé les réponses des autres posts mais je ne sais pas comment les appliquer dans mon cas.

Voici mon code de travail :

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

Supposons que je veuille une fonction :

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

// Give staffMap a data here

Je n'arrive pas à trouver de tutoriel suffisant pour faire une fonction qui retourne std::map en c++. J'espère que quelqu'un pourra m'aider. J'espère que quelqu'un pourra m'aider.

4voto

YayCplusplus Points 560

Je n'ai pas trouvé de tutoriel suffisant pour créer une fonction qui retourne std::map en c++. J'espère que quelqu'un pourra m'aider

Vous devez spécifier le type exact, std::map<int, std::string> :

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

Si vous pouvez utiliser C++14, utilisez auto comme alternative :

auto GetStaffMap()
{
    return staffMap;
}

1voto

slaughter98 Points 620

L'exemple ci-dessous montre comment créer une fonction en C++ qui renvoie une carte.

// 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;
}

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X