1

I have the following structure.

struct Tourist { string name; string surname; string sex; }; 

I would like to sort tourists by families.

int getMinRoomsAmount(Tourist touristsList[]) { map<string, Tourist[]> families; for (int i=0; i < 40; i++) { families[touristsList[i].surname] = // to append the array with the tourist } return 0; } 

Is it possible to have a map where the key is a string, and the value is an array of structures? And how can I append the array with new entries?

10
  • Recommended reading: kalzumeus.com/2010/06/17/… Commented Jun 14, 2020 at 21:12
  • 1
    No, arrays cannot be objects. But std::vectors can be. This is what std::vector is for, so what you want is std::map<std::string, std::vector<Tourist>>, however if your goal is to sort it by surname, you don't even need that. just put everything in a single std::vector and use std::sort with a custom comparator lambda. Much simpler, and easier. Commented Jun 14, 2020 at 21:13
  • @SamVarshavchik Arrays are absolutely objects. Commented Jun 14, 2020 at 21:14
  • If arrays are objects, then a=b would work, when both a and b are arrays. Obviously, that's not the case. Commented Jun 14, 2020 at 21:14
  • 1
    The default assignment operator gets deleted only in very few circumstances. Specific exceptions don't prove or disprove the rule. Commented Jun 14, 2020 at 21:18

3 Answers 3

3
  • Map : You could use a map of string and vector of Tourist - map<string, std::vector<Tourist> > families; .
  • Insertion : For adding a new element to a family, just use push_back() method of vector as - families[touristsList[i].surname].push_back(touristsList[i]); . This statement will simply add the family(Tourist struct) to the map with key of surname.

Below is a working demo of your program -

#include <iostream> #include<map> #include<vector> struct Tourist { std::string name; std::string surname; std::string sex; }; int getMinRoomsAmount(std::vector<Tourist> touristsList) { std::map<std::string, std::vector<Tourist> > families; for (int i=0; i < 3; i++) { // to append the array with the tourist families[touristsList[i].surname].push_back(touristsList[i]); } // iterating over the map and printing the Tourists families-wise for(auto it:families){ std::cout<<"Family "<<it.first<<" : \n"; for(auto family : it.second){ std::cout<<family.name<<" "<<family.surname<<" "<<family.sex<<std::endl; } std::cout<<"\n-------\n"; } return 0; } int main() { // making 3 struct objects just for demo purpose Tourist t1={"a1","b1","m"}; Tourist t2={"a2","b1","f"}; Tourist t3={"a3","b3","m"}; // inserting the objects into vector and then passing it to the function std::vector<Tourist>t={t1,t2,t3}; getMinRoomsAmount(t); } 

I have just included 3 Tourist objects for demo purpose. You can modify the code to suit your needs. I have used vectors instead of array because they are more efficient and you can dynamically push/pop as per the user inputs later on if you want to modify the program.

Hope this helps !

Sign up to request clarification or add additional context in comments.

Comments

1

You really want to stay away from arrays, especially when using std::map. The std::map will copy your structure and arrays don't copy well.

Here is the definition of a map with value as an std::vector:

std::map<std::string, std::vector<Tourist>> 

Here is how to add to the map:

std::vector<Tourist> database; Tourist t1{"x", "x", "x"}; Tourist t2{"y", "y", "y"}; Tourist t3{"z", "z", "z"}; database.pushback(t1); database.pushback(t2); database.pushback(t3); // Check this out: std::map<std::string, std::vector<Tourist>> visitors; visitor["Italy"] = database; 

Comments

-3

Use a map of string→vector<Tourist>.

Then use the vectors in the normal way, e.g. push_back.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.