-1

I am trying to write a code to make a vector of maps to store parts of string by splitting it. This code is giving long compilation errors, i am not able to understand what is the problem. Problem is with initialization way

#include <bits/stdc++.h>
using namespace std;

vector<string> split(string phrase, string delimiter){
    vector<string> list;
    string s = phrase;
    size_t pos = 0;
    string token;
    while ((pos = s.find(delimiter)) != string::npos) {
        token = s.substr(0, pos);
        list.push_back(token);
        s.erase(0, pos + delimiter.length());
    }
    list.push_back(s);
    return list;
}

int main() {
    string line = "tunilib;sebesta;prog lang;14";
    vector<string> splitstring = split(line, ";");
    vector< map<string,string,string,string> > elements;
    map<string,string,string,string> element;
    element["library"] = splitstring[0];
    element["author"] = splitstring[1];
    element["title"] = splitstring[2];
    element["reservation"] = splitstring[3];
    elements.push_back(element);
    for(auto i:splitstring) cout<<i<<" ";
    cout<<"success";
    return 0;
}
rohillasarthak
  • 125
  • 4
  • 14
  • 3
    What do you expect `map` to do ? – Sid S Mar 06 '19 at 05:05
  • 2
    Don't use `#include ` ([why](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)) and avoid using `using namespace std;` ([why](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). Putting the two together is madness. MADNESS I say! **MADNESS!**. – user4581301 Mar 06 '19 at 05:05
  • i want to store key value pairs and like a dict and add to a vector – rohillasarthak Mar 06 '19 at 05:06
  • There's a standard library container called `std::list`, and since you're porting the entire standard library into the global namespace with the first two lines of your program, you will have problems with your variable that has the same name. – BessieTheCookie Mar 06 '19 at 05:13
  • but that isn't the case, problem is due to vector declaration, i have tested removing this and implementation, code runs good. @FeiXiang – rohillasarthak Mar 06 '19 at 05:17
  • Turns out you don't get a compiler error in this case. It's not a good idea though, for the reasons in user4581301's comment. – BessieTheCookie Mar 06 '19 at 05:23
  • How are you intending to use this `map`? `map` is definitely not quite right, but it's hard to make a good suggestion on what the structure should be without a better idea of what you plan to do with it. – user4581301 Mar 06 '19 at 05:26
  • i need to create a list of key value pairs, getting values by splitting input string, I thought i could do this by creating a vector of map @user4581301 – rohillasarthak Mar 06 '19 at 05:45
  • Thank you. `map` gets your key-value pairs and may give you the result you want. [Documentation for `map`](https://en.cppreference.com/w/cpp/container/map). The crawl of error messages you are getting is likely the result of `map` telling the compiler that the comparator function is `std::string` rather than a class or function that performs a comparison on two strings and the allocator is `std::string` rather than a function or class that will allocate storage for the pair. – user4581301 Mar 06 '19 at 05:51
  • Thanks, @user4581301, yeah same problem you mentioned solved now. – rohillasarthak Mar 06 '19 at 06:35

1 Answers1

1

Error was in declaration, simply change,

map<string,string,string,string>

to

map<string,string>

as i wanted to create a list of key value pairs.

@user4581301 thanks for comment

rohillasarthak
  • 125
  • 4
  • 14