由于本人要接手一项C++方面 的工作。由于不会C++,不过做过JAVA 以及一些web方面的开发,加之时间比较短。所以需要速成,于是学习笔记也基本都是用代码代替。

//范例资源文件
/*****************************************************************************************
Test.txt
:
tom 123456789
lilei 234567891
zhangsan 345678912
tom 456789123
xiaohe 567891234                                                                    
******************************************************************************************/
//未用名字空间,使用vector 进行遍历
/******************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
int _tmain(int argc,_TCHAR* argv[])
{
std::string strtmp;
std::ifstream fs("Test.txt");
std::vector<std::string> vect;
while(getline(fs,strtmp,'\n'))
{
vect.push_back(strtmp.substr(0,strtmp.find(" ")));
}
for(std::vector<std::string>::size_type index = 0; index < vect.size();index++)
{
std::cout << vect[index]   << std::endl;
}
return 0;
}
******************************************************************************************/
//使用名字空间,使用vector进行遍历
/******************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int _tmain(int argc,_TCHAR* argv[])
{
string strtmp;
ifstream fs("Test.txt");
vector<string> vect;
while(getline(fs,strtmp,'\n'))
{
vect.push_back(strtmp.substr(0,strtmp.find(" ")));
}
for(int index = 0; index < vect.size();index++)
{
cout << vect[index]   << endl;
}
return 0;
}
******************************************************************************************/
//利用迭代器,对vector进行遍历
/******************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int _tmain(int argc,_TCHAR* argv[])
{
 string strtmp;
 ifstream fs("Test.txt");
 vector<string> vect;
 while(getline(fs,strtmp,'\n'))
 {
  vect.push_back(strtmp.substr(0,strtmp.find(" ")));
 }
 vector<string>::iterator it = vect.begin();

 for(;it != vect.end();it++)
 {
  cout << *it  << endl;
 }
 return 0;
}
******************************************************************************************/
//使用map 进行遍历
/******************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <map>

using namespace std;

int _tmain(int argc,_TCHAR* argv[])
{
 string strtmp;
 ifstream fs("Test.txt");
 map<string,string>  map_temp;
 string::size_type index = string::npos;

 while(getline(fs,strtmp,'\n'))
 {
 index = strtmp.find("");
 map_temp[strtmp.substr(0,index)] = (strtmp.substr(++index));
 }
 map<string,string> ::iterator it = map_temp.begin();

 for(; it != map_temp.end(); it++)
 {
  cout << it->first << " = " << it->second << endl;
 }
 return 0;
}
******************************************************************************************/
//使用for_each 利用重载操作符 进行map遍历
/******************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <map>
#include <iterator>
#include <algorithm>

using namespace std;
class showIiem
{
public:
void operator()(const map<string,string>::value_type& value)
{
cout << value.first << " = " << value.second << "\n";
};

};
int _tmain(int argc,_TCHAR* argv[])
{
 string strtmp;
 ifstream fs("Test.txt");
 map<string,string>  map_temp;
 string::size_type index = string::npos;

 while(getline(fs,strtmp,'\n'))
 {
  index = strtmp.find("");
  map_temp[strtmp.substr(0,index)] = (strtmp.substr(++index));
 }
 showIiem show;
 for_each(map_temp.begin(),map_temp.end(),show);
 return 0;
}
******************************************************************************************/
// 利用copy,使用重载操作符 进行map遍历
/******************************************************************************************
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;
namespace std
{
 std::ostream& operator<<(std::ostream& os, const std::map<std::string,std::string>::value_type& value)
 {
  os << value.first << "= " << value.second;
  return os;
 }
}
class ShowValue
{
public:
 ShowValue(std::ostream& os):m_os(os)
 {
 }

 void operator()(const std::map<std::string,std::string>::value_type& value)
 {
  m_os << value.first << "= " << value.second << "\n";
 }
 std::ostream& m_os;
};
typedef std::ostream_iterator<std::map<std::string,std::string>::value_type> ositertype;
std::ostream& operator<<(std::ostream& os, const std::map<std::string,std::string>& value)
{
 std::for_each(value.begin(), value.end(), ShowValue(os));
 return os;
}
int _tmain(int argc,_TCHAR* argv[])
{
 std::string strtmp;
 std::fstream in("Test.txt");
 std::map<std::string,std::string>  map_1;
 std::string::size_type idx = std::string::npos;
 while(std::getline(in, strtmp, '\n'))
 {
  idx = strtmp.find(' ');
  map_1[strtmp.substr(0, idx)] = strtmp.substr(++idx);
 }
 ositertype os_iter(std::cout, "\n");
 std::copy(map_1.begin(), map_1.end(), os_iter);
 return 0;
}

posted on 2008-04-08 23:46 -274°C 阅读(3990) 评论(0)  编辑  收藏 所属分类: C++

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(21)

随笔分类(265)

随笔档案(242)

相册

JAVA网站

关注的Blog

搜索

  •  

积分与排名

  • 积分 - 908979
  • 排名 - 40

最新评论