Difference between revisions of "Effective Analysis Programming Part 2"
From Gridkaschool
m (→C++ Templates) |
m (→exercise 2: read paramerters) |
||
Line 12: | Line 12: | ||
</source> |
</source> |
||
− | The following code is an example how to use |
+ | The following code is an example how to use ''readparameters'': |
<source lang="cpp"> |
<source lang="cpp"> |
||
#define MAIN_CPP |
#define MAIN_CPP |
||
Line 49: | Line 49: | ||
} |
} |
||
</source> |
</source> |
||
+ | |||
+ | * Run the code, add more parameters to the text file, read a boolean parameter. |
||
+ | * Read the code of the class ''readparamerters'' (you may skip the parsing part in the constructor). |
||
+ | **get and get_vector are template functions. Why are there specializations for bool and string? |
||
+ | **Why is the map tabular mutual? |
||
+ | **What are the requirements for types to work with readparameters? |
||
+ | **Why is the default constructor privat? |
Revision as of 17:29, 23 August 2013
C++ Templates
exercise 1: pair
exercise 2: read paramerters
The library readparamertes can be used to read values from a text file like:
# GKS 2013
answer 42
parser_test "\\\"" "#" #should result in \" and #
useful yes
The following code is an example how to use readparameters:
#define MAIN_CPP
#include <iostream>
#include <exception>
#include "readparameters.h"
using namespace std;
int main(int argc,char **argv){
//check arguments
if (argc!=2) {
cerr<<"Usage: ./params <paramfile>"<<endl;
return 1;
}
//get input filename from parameter file
int answer;
vector<string> v;
try {
readparameters rp(argv[1]);
answer = rp.get<int>("answer");
v=rp.get_vector<string>("parser_test");
cout<<answer<<endl;
rp.print_tabular(); //debug output
rp.get<long>("Atlantis"); //exception
}
catch (exception& e) {
cerr<<e.what()<<endl;
return 1;
}
return 0;
}
- Run the code, add more parameters to the text file, read a boolean parameter.
- Read the code of the class readparamerters (you may skip the parsing part in the constructor).
- get and get_vector are template functions. Why are there specializations for bool and string?
- Why is the map tabular mutual?
- What are the requirements for types to work with readparameters?
- Why is the default constructor privat?