C++ string::npos,结合find,或者sscanf



    首先getline

getline(istream &in, string &s)

从输入流读入一行到string s

•功能:
–从输入流中读入字符,存到string变量
–直到出现以下情况为止:
•读入了文件结束标志
•读到一个新行
•达到字符串的最大长度
–如果getline没有读入字符,将返回false,可用于判断文件是否结束
  1. #include<iostream>  
  2. #include<fstream>  
  3. #include<string>  
  4.   
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     string buff;  
  10.     ifstream infile;  
  11.     ofstream outfile;  
  12.     cout<<"Input file name: "<<endl;  
  13.     cin>>buff;  
  14.     infile.open(buff.c_str());  
  15.   
  16.     if(!infile)  
  17.         cout<<"error"<<buff<<endl;  
  18.       
  19.     cout<<"Input outfile name: "<<endl;  
  20.     cin>>buff;  
  21.     outfile.open(buff.c_str());  
  22.       
  23.     while(getline(infile, buff))  
  24.         outfile<<buff<<endl;  
  25.   
  26.     infile.close();  
  27.     outfile.close();  
  28.     return 0;  
  29.   
  30. }  
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
	string buff;
	ifstream infile;
	ofstream outfile;
	cout<<"Input file name: "<<endl;
	cin>>buff;
	infile.open(buff.c_str());

	if(!infile)
		cout<<"error"<<buff<<endl;
	
	cout<<"Input outfile name: "<<endl;
	cin>>buff;
	outfile.open(buff.c_str());
	
	while(getline(infile, buff))
		outfile<<buff<<endl;

	infile.close();
	outfile.close();
	return 0;

}


      

http://blog.csdn.net/stpeace/article/details/13069403

设1.txt文件内容如下:

name = baidu
url = www.baidu.com

 

       看程序:

  1. #include <fstream>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     ifstream in("1.txt");  
  9.     string filename;  
  10.     string line;  
  11.     string s;  
  12.     string :: size_type pos;  
  13.   
  14.     if(in) // 有该文件  
  15.     {  
  16.         while (getline (in, line)) // line中不包括每行的换行符  
  17.         {   
  18.             pos = line.find("name");  
  19.             if(pos != string :: npos)  
  20.             {  
  21.                 s = line.substr(pos + strlen("name") + strlen(" = "));  
  22.                 cout << s.c_str() << endl;  
  23.             }  
  24.   
  25.             pos = line.find("url");  
  26.             if(line.find("url") != string :: npos)  
  27.             {  
  28.                 s = line.substr(pos + strlen("url") + strlen(" = "));  
  29.                 cout << s.c_str() << endl;  
  30.             }  
  31.   
  32.         }  
  33.     }  
  34.     else // 没有该文件  
  35.     {  
  36.         cout <<"no such file" << endl;  
  37.     }  
  38.   
  39.     return 0;  
  40. }  
#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	ifstream in("1.txt");
	string filename;
	string line;
	string s;
	string :: size_type pos;

	if(in) // 有该文件
	{
		while (getline (in, line)) // line中不包括每行的换行符
		{ 
			pos = line.find("name");
			if(pos != string :: npos)
			{
				s = line.substr(pos + strlen("name") + strlen(" = "));
				cout << s.c_str() << endl;
			}

			pos = line.find("url");
			if(line.find("url") != string :: npos)
			{
				s = line.substr(pos + strlen("url") + strlen(" = "));
				cout << s.c_str() << endl;
			}

		}
	}
	else // 没有该文件
	{
		cout <<"no such file" << endl;
	}

	return 0;
}

     结果为:

baidu

www.baidu.com

 

    当然也可以用sscanf, 如下:

  1. #include <fstream>  
  2. #include <string>  
  3. #include <iostream>  
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     ifstream in("1.txt");  
  9.     string filename;  
  10.     string line;  
  11.     char s1[1024] = {0};  
  12.     char s2[1024] = {0};  
  13.   
  14.     if(in) // 有该文件  
  15.     {  
  16.         while (getline (in, line)) // line中不包括每行的换行符  
  17.         {   
  18.             if(2 == sscanf(line.c_str(), "%s = %s", s1, s2))  
  19.             {  
  20.                 cout << s2 << endl;  
  21.             }  
  22.             else  
  23.             {  
  24.                 return 1;  
  25.             }  
  26.               
  27.         }  
  28.     }  
  29.     else // 没有该文件  
  30.     {  
  31.         cout <<"no such file" << endl;  
  32.     }  
  33.   
  34.     return 0;  
  35. }  



拓展:

查找两个字符串中的公共字符;

返回公共字符的索引;

  1. #include<iostream>  
  2. #include<string>  
  3.   
  4. using namespace std;  
  5.   
  6. int main()  
  7. {  
  8.     string s = "**Gteate Wall**!";  
  9.     string t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";  
  10.     cout<<"s: "<<s<<endl;  
  11.     cout<<"t: "<<t<<endl;  
  12.   
  13.     int first=s.find_first_of(t);  
  14.   
  15.     if(first == string::npos){  
  16.         cout<<"s中所有字符均不在t中"<<endl;  
  17.     }else {  
  18.         cout<<"s中出现在t中的字符的第一个字符:"<<s[first]<<endl;  
  19.     }  
  20.   
  21.     int last = s.find_last_of(t);  
  22.     if(last == string::npos){  
  23.         cout<<"s中所有字符均不在t中"<<endl;  
  24.         return 1;  
  25.     }else {  
  26.         cout<<"s中出现在t的字符的最后一个字符:"<<s[last]<<endl;  
  27.         return 1;  
  28.     }  
  29.   
  30. }  
<u>#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s = "**Gteate Wall**!";
	string t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	cout<<"s: "<<s<<endl;
	cout<<"t: "<<t<<endl;

	int first=s.find_first_of(t);

	if(first == string::npos){
		cout<<"s中所有字符均不在t中"<<endl;
	}else {
		cout<<"s中出现在t中的字符的第一个字符:"<<s[first]<<endl;
	}

	int last = s.find_last_of(t);
	if(last == string::npos){
		cout<<"s中所有字符均不在t中"<<endl;
		return 1;
	}else {
		cout<<"s中出现在t的字符的最后一个字符:"<<s[last]<<endl;
		return 1;
	}

}
</u>

string::npos的一些说明

string::npos 的一些说明

一、定义

std:: string ::npos的定义:

static const size_t npos = -1;

表示 size_t 的最大值( Maximum value for size_t ) ,如果对 -1 表示size_t的最大值有疑问可以采用如下代码验证:

#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
{
    size_t npos = -1;
    cout << "npos: " << npos << endl;
    cout << "size_t max: " << numeric_limits<size_t>::max() << endl;
}

在我的PC上执行结果为:

                 npos:           4294967295

                 size_t max:  4294967295

可见他们是相等的,也就是说npos表示size_t的最大值

二、使用

2.1 如果作为一个 返回值 (return value) 表示没有找到匹配项 ,例如:

#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
{
  string filename = "test";
  cout << "filename : " << filename << endl;

  size_t idx = filename.find('.');   //作为return value,表示没有匹配项
  if(idx == string::npos)	
  {
    cout << "filename does not contain any period!" << endl;
  }
}
2.2 但是string::npos作为string的成员函数的一个 长度参数 时,表示“ 直到字符串结束 (until the end of the string)”。例如:
tmpname.replace(idx+1, string::npos, suffix);

这里的string::npos就是一个长度参数,表示直到字符串的结束,配合idx+1表示,string的剩余部分。

#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
{
  string filename = "test.cpp";
  cout << "filename : " << filename << endl;

  size_t idx = filename.find('.');   //as a return value
  if(idx == string::npos)	
  {
    cout << "filename does not contain any period!" << endl;
  }
  else
  {
    string tmpname = filename;
    tmpname.replace(idx + 1, string::npos, "xxx"); //string::npos作为长度参数,表示直到字符串结束
    cout << "repalce: " << tmpname << endl;
  }
}

执行结果为:

filename:test.cpp

replace: test.xxx      


给下面代码每一行给上注释并说明这段代码的意思#include "pch.h" #include "FinBudgetSupport.h" #include <map> time_t StringToDateTime(char* str) { tm tm_; int year, month, day, hour, min, sec; afxDump << str << "\n\n\n\n"; sscanf_s(str, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &min, &sec); tm_.tm_year = year - 1900; tm_.tm_mon = month - 1; tm_.tm_mday = day; tm_.tm_hour = hour; tm_.tm_min = min; tm_.tm_sec = sec; tm_.tm_isdst = -1; time_t t_ = mktime(&tm_); return t_; } CString DateTimeToString(time_t _time) { tm *_tm = new tm(); gmtime_s(_tm ,&_time); CString t_str; t_str.Format(_T("%d-%d-%d %d:%d:%d"), _tm->tm_year+1900, 1+_tm->tm_mon, _tm->tm_mday, _tm->tm_hour , _tm->tm_min, _tm->tm_sec); delete _tm; return t_str; } CString _toCString(double _value) { CString t_str; t_str.Format(_T("%lf"), _value); return t_str; } CString _toCString(int _value) { CString t_str; t_str.Format(_T("%d"), _value); return t_str; } double _toDouble(CString _str) { return _ttof(_str); } char* CStringToCharArray(CString str) { int str_len = WideCharToMultiByte(CP_ACP, 0, str, str.GetLength(), NULL, 0, NULL, NULL);//计算字节数 char* CharArray = new char[str_len + 1]; WideCharToMultiByte(CP_ACP, 0, str, str.GetLength(), CharArray, str_len, NULL, NULL); CharArray[str_len] = '\0'; return CharArray; } void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c) { std::string::size_type pos1, pos2; pos2 = s.find(c); pos1 = 0; while (std::string::npos != pos2) { v.push_back(s.substr(pos1, pos2 - pos1)); pos1 = pos2 + c.size(); pos2 = s.find(c, pos1); } if (pos1 != s.length()) v.push_back(s.substr(pos1)); }
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值