#include <iostream> #include <string> #include <map> #include <cctype> // isalpha(),etc. using namespace std; map < string, double > lookup; int main () { lookup["pi"] = 3.1415926535; lookup["e"] = 2.718281828459045; printf("%f\n",lookup["e"]); }
/* */ #include <iostream> #include <map> #include <string> using namespace std; class INFO { public: string desc; double value; }; int main () { typedef map < string, INFO > LOOKUP; INFO info; LOOKUP lookup; lookup["pi"].value = 3.1415926535; lookup["pi"].desc = "Pi to several digits"; lookup["e"].value = 2.718281828459045; lookup["e"].desc = "lim as x-> inf (1+x)^(1/x)"; cout << lookup["e"].desc << "= " << lookup["e"].value << endl; }
/* Simple: read from a file and write to cout */ #include<iostream> #include<iterator> #include <fstream> using namespace std; int main (int argc, char **argv) { string infile; if (argc == 2) { infile = argv[1]; } else { cout << "need filename" << endl; return 1; } ifstream is (infile.c_str ()); istream_iterator<string> ii(is); istream_iterator<string> eos; while( ii != eos ) { cout << *ii << endl; ++ii; } }
#include <string> #include <sstream> #include <iostream> #include <algorithm> using namespace std; int main(void) { string s1; string::iterator p; string::size_type st; stringstream ss; int i; s1 = "Initial string "; s1 += " more data to append"; st = s1.find("append"); if (st == string::npos) { cout << "not found " << endl; } else { cout << "s1: " << s1 << endl; cout << "Results of s1.find: " << s1.substr(st) << endl; cout << "Results of s1.substr(0,3): " << s1.substr(0, 3) << endl; cout << "Size of string:" << s1.size() << endl; } for (i = 0; i < 12; i++) { ss << "i=" << i << endl; } s1 += ss.str(); cout << "s1+=ss.str() Results of appending the output of ss to s1:" << endl; cout << s1 << endl; cout << "Size of s1:" << s1.size() << endl; }