1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <typeinfo>
int dict_value(std::map<std::string, int> &temp, std::string name){
std::map<std::string, int>::const_iterator iter = temp.find(name);
if (iter != temp.end()) {
return iter->second;
}
}
std::vector<std::string> split(std::string s){
std::string buf;
std::stringstream ss(s);
std::vector<std::string> tokens;
while (ss >> buf)
tokens.push_back(buf);
return tokens;
}
int str_to_int(std::string s){
int i;
std::istringstream convert(s);
if ( !(convert >> i) )
i = 0;
return i;
}
std::map<std::string, int> read(){
std::string s = "";
std::ifstream filer;
filer.open("username.txt");
while(getline(filer, s)){
//split last index and the rest
std::vector<std::string> list = split(s);
std::string last = list.back();
list.pop_back();
std::string name;
for (int i=0; i<list.size(); i++)
name += list[i] + ' ';
int num;
num = str_to_int(last);
/*
std::cout << num << std::endl;
std::cout << name << std::endl;
std::cout << '\n';
*/
std::map<std::string, int> dict;
dict.insert(std::make_pair(name, num));
return dict;
}
}
void write (std::string name, int num){
std::ofstream filer;
filer.open ("username.txt");
filer << name << " " << num << '\n';
filer.close();
}
int main(int argc, char *argv[])
{
std::ofstream filer;
std::map<std::string, int> dict = read();
for (std::map<std::string, int>::iterator iter = dict.begin(); iter != dict.end(); ++iter){
std::cout << iter->first << std::endl;
std::cout << iter->second << std::endl;
}
//dict.insert(std::make_pair("metulburr",23));
//write("metulburr", 25);
//write("raltec", 87);
//read();
/*
std::cout << dict_value(dict, "metulburr") << std::endl;
std::cout << dict_value(dict, "a") << std::endl;
*/
/*
std::cout << "argc is " << argc << std::endl;
std::cout << "argv[0] is " << argv[0] << std::endl;
std::cout << "argv[1] is " << argv[1] << std::endl;
std::cout << "argv[2] is " << argv[2] << std::endl;
*/
}
|