fork download
  1. #include<iostream>
  2. #include<fstream>
  3. #include<sstream>
  4. #include<string>
  5. #include<vector>
  6. #include<algorithm>
  7.  
  8. static const char* filename = "roman2.txt";
  9.  
  10. typedef std::vector<std::string> stringv;
  11.  
  12. namespace stv {
  13. stringv& split( stringv& v, std::string& src, char delim = ','){
  14. std::istringstream iss(src);
  15. std::string temp;
  16. while( std::getline(iss,temp,delim) ){
  17. v.push_back(temp);
  18. }
  19. }
  20.  
  21. stringv split( std::string& src, char delim = ','){
  22. stringv lv;
  23. return split(lv,src,delim);
  24. }
  25.  
  26. template <typename value_type>
  27. struct joinf {
  28. value_type& val_;
  29. value_type& delim_;
  30. joinf( value_type& val, value_type& delim ) : val_(val), delim_(delim) {}
  31. void operator ()( value_type& val ){ val_ += ( val + delim_ ); }
  32. };
  33.  
  34. std::string& join( std::string& s, stringv& v, const char* delim = ", "){
  35. std::string sdelim(delim);
  36. joinf<std::string> j(s,sdelim);
  37. std::for_each(v.begin(),v.end(), j);
  38. return j.val_;
  39.  
  40. }
  41.  
  42. std::string join( stringv& v, const char* delim = ", "){
  43. std::string lv;
  44. return join(lv,v,delim);
  45. }
  46. }
  47.  
  48. int main(void)
  49. {
  50. using namespace std;
  51.  
  52. cout << "郵便番号を入力してください" << endl;
  53.  
  54. string bango; cin >> bango;
  55.  
  56. ifstream ifs( filename, ios::in );
  57.  
  58. if( ifs.fail() ){
  59. cout << "ファイルを開けませんでした。:" << filename << endl;
  60. return 0;
  61. }
  62.  
  63. stringv v;
  64. string temp;
  65. bool found = false;
  66. while( !found && std::getline(ifs,temp) ){
  67. v.clear();
  68. stv::split(v,temp,'\t');
  69. found = ( v.at(1) == bango );
  70. }
  71. ifs.close();
  72.  
  73. cout << ( found ? stv::join(v,", ") : "見つかりませんでした。" )
  74. << endl;
  75. return 0;
  76. }
  77.  
Success #stdin #stdout 0.01s 2872KB
stdin
Standard input is empty
stdout
郵便番号を入力してください
ファイルを開けませんでした。:roman2.txt