-4

There is a map with map<string, vector<string>> buses; structure, and I have a string, so I need to find out how to check string contains in map vector.

string stop;
cin >> stop;

if (buses.find(stop) != buses.end()) {
    cout << "Yes";
} else {
    cout << "No";
}

When I get a stop string I need to check is there in map's values (not keys, to be more precise in map.second) a string equals to stop string, if yes I need to print out all the keys containing stop element in values. { {"bus#1", {"stop#1", "stop#2", "stop#3"}}, ... }

Bootuz
  • 530
  • 3
  • 6
  • 16

2 Answers2

1

One way to do this is to loop over all map elements and then use std::find on the value vectors. This is really inefficient though.

Instead, you should use a std::multimap<std::string, std::string> (or std::unordered_multimap if you don't need the sorting) and use the stops as key. Then add all buses that use said stop in that key and use find if you want to get all buses for that stop.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
-1

I suppose this is what you are trying to do.

#include <map>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    map<string, vector<string>> buses;

    buses["some stop"] = vector<string>();

    if (buses.find("some stop") != buses.end()) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}

Are you sure map is the data structure you want for this task?

Otherwise you may want to check out std::set if you only want to see if the stop is in the data structure.

Orvis
  • 1