I needed to create some kind of a bus route handbook (just practicing around), so I wrote this.
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
vector<string> GenWay(int length){
vector<string> res;
string stop;
for(int i = 0; i<length; i++){
cin >> stop;
res.push_back(stop);
}
return res;
}
int isVinMap(vector<string> vs, map<int,vector<string>> miv){
for(const auto& [key, vvalue]:miv){
if(vvalue == vs) return key;
}
return 0;
}
int main(){
int q=0;
cin >> q;
map<int,vector<string>> ways;
string stop="";
int command=0;
int next_way = 1; //last+1 //last = 0
for (int i = 0; i < q; i++){
cin >> command;
vector<string> new_way = GenWay(command);
int check = isVinMap(new_way,ways);
cout << check << !static_cast<bool>(check); //Debug code
if(!static_cast<bool>(check)){
cout << "New bus " << next_way << endl;
ways[next_way] = GenWay(command); //next_way is not controlled by user
next_way++;
} else{
cout << "Already exists for " << check << endl;
}
}
}
Here what it should do:
First input must be the quantity of coming commands. (q) (e.g. 4
) (no output)
There's only one command that can be processed after first, and it has next format: number_of_bus_stops stop1 stop2 stop3...
(e.g. 2 m k
). It adds an entry to map<int,vector<string>> ways
, about the marchrute (The number of marchrute is not defined by user, and is next_way
, which should increase after each new entry). If the same marchrute appear in other entries, it tells that it already exist and print the number of marchrute which contains this way (isVinMap
method checks that and gives the number of marchrute)(ways with different order of stops are different), and tells that new bus created by saying (e.g. New bus 1
) if adding of an entry is succesful.
However it doesn't work as it supposed to. Output is nearly unpredictable. I'm working in EclipseIDE on Winx64 system and got this output:
>4
>2 m k
01New bus 1
>3 k r s
01New bus 2
20Already exists for 2
20Already exists for 2
Seems like cycle goes on for two more times, after first two commands, but it doesn't invite me to input anything.
And, of course, any criticism of the code is appreciated! and sorry for lack of documentation and commentaries in the code, it wasn't supposed that i would have needed to work for that long on this one.