kuafu
kuafu

Reputation: 1486

cpp about for_each usage

here is my code,I want to read config file using c++ my code is here:

//myutils.h

#include <string>
#include <map>
using namespace std;

void print(pair<string,string> &p);

void read_login_data(char *login_data,map<string,string> &data_map); 

here is the myutils.cpp

//myutils.cpp
#include <fstream>
#include <string>
#include <map>
#include "myutils.h"

using namespace std;

void print(pair<string,string> &p)
{
        cout<<p.second<<endl;
}


void read_login_data(char *login_data,map<string,string> &data_map)
{
    ifstream infile;
    string config_line;
    infile.open(login_data);
    if (!infile.is_open())
    {
        cout << "can not open login_data";
        return false;

    }
    stringstream sem;
    sem << infile.rdbuf();
    while(true)
    {
        sem >> config_line;
        while(config_line)
        {
            size_t pos = config_line.find('=');
            if(pos == npos) continue;
            string key = config_line.substr(0,pos);
            string value = config_line.substr(pos+1);
            data_map[key]=value;

        }
    }


}

and my test.cpp code:

#include <iostream>
#include <map>
#include "myutils.h"

using namespace std;

int main()
{
    char login[] = "login.ini";
    map <string,string> data_map;

    read_login_data(login,data_map);
    for_each(data_map.begin(),data_map.end(),print);

    //cout<< data_map["BROKER_ID"]<<endl;

}

the config file is:

BROKER_ID=66666
INVESTOR_ID=00017001033

and when I compile it using :g++ -o test test.cpp myutils.cpp,the output is:

young001@server6:~/ctp/ctp_github/trader/src$ g++ -o test test.cpp myutils.cpp
In file included from /usr/include/c++/4.6/algorithm:63:0,
                 from test.cpp:3:
/usr/include/c++/4.6/bits/stl_algo.h: In function ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >, _Funct = void (*)(std::pair<std::basic_string<char>, std::basic_string<char> >&)]’:
test.cpp:15:48:   instantiated from here
/usr/include/c++/4.6/bits/stl_algo.h:4379:2: error: invalid initialization of reference of type ‘std::pair<std::basic_string<char>, std::basic_string<char> >&’ from expression of type ‘std::pair<const std::basic_string<char>, std::basic_string<char> >’

it seems about reference of pair<>,how to modify to work?

Upvotes: 2

Views: 217

Answers (1)

MobA11y
MobA11y

Reputation: 18860

I believe this line:

void print(pair<string,string> &p)

should be

void print(pair<const string,string> &p)

In maps the "key" part of the pair is a constant, and only the second item can be modified. It is complaining that you are not maintaing this when reading each pair individually in your function declaration, and therefore not guaranteeing that you leave the key portion of the pair untampered with.

EDIT:

Your read looping is kinda wierd. I think it's okay, but it's bad style. I think this, or something close to it, will work better for you.

while(getline(infile, config_line)) {
    size_t pos = config_line.find('=');
    if(pos != string::npos) {
        string key = config_line.substr(0,pos);
        string value = config_line.substr(pos+1);
        data_map[key]=value;
    } else {
        cout << "BAD INPUT PAIR" << endl; //throw exception?
    }
}

The above works with an input file that looks like this

blah = bigblah
no equals on this one

Upvotes: 4

Related Questions