您的位置:首页 > 编程语言 > C语言/C++

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

2016-07-18 10:53 721 查看

    首先getline

getline(istream &in, string &s)

从输入流读入一行到string s

•功能:
–从输入流中读入字符,存到string变量
–直到出现以下情况为止:
•读入了文件结束标志
•读到一个新行
•达到字符串的最大长度
–如果getline没有读入字符,将返回false,可用于判断文件是否结束

[cpp]
view plain
copy
print?





#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;  
  
}  
#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

 

       看程序:

[cpp]
view plain
copy
print?

#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;  
}  



#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)) /
e055
/ 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, 如下:

[cpp]
view plain
copy
print?

#include <fstream>  
#include <string>  
#include <iostream>  
using namespace std;  
  
int main()  
{  
    ifstream in("1.txt");  
    string filename;  
    string line;  
    char s1[1024] = {0};  
    char s2[1024] = {0};  
  
    if(in) // 有该文件  
    {  
        while (getline (in, line)) // line中不包括每行的换行符  
        {   
            if(2 == sscanf(line.c_str(), "%s = %s", s1, s2))  
            {  
                cout << s2 << endl;  
            }  
            else  
            {  
                return 1;  
            }  
              
        }  
    }  
    else // 没有该文件  
    {  
        cout <<"no such file" << endl;  
    }  
  
    return 0;  
}  

拓展:

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

返回公共字符的索引;

[cpp]
view plain
copy
print?





#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>#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的一些说明

时间 2014-05-10 14:54:24
CSDN博客

原文  http://blog.csdn.net/devil_pull/article/details/25478525 主题
技术

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      
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: