首页 > 其他 > 详细

常用代码库

时间:2015-07-02 11:55:32      阅读:242      评论:0      收藏:0      [点我收藏+]

 

目录:

1.从txt中读一行

2. 分割string字符串

=============================================================

 

1. 从txt中读一行

 1    cout<<"input the filename:"<<endl;
 3    string filename;
 5    cin>>filename;
 7    ifstream infile(filename.c_str());
 9    string temp;
11    while(getline(infile,temp)){
15       cout<<temp<<endl;
17    }

 

2. 分割string字符串

 

//
vector<string> split(string str, string pattern) {
    string::size_type pos;
    vector<string> result;
    str += pattern; //在最后加上分割类型,扩展字符串以方便操作
    int size = str.size();
    for (int i = 0; i < size; i++) {
        pos = str.find(pattern, i);
        if (pos < size) {
            string s = str.substr(i, pos - i);
            result.push_back(s);
            i = pos + pattern.size() - 1;
        }
    }
    return result;
}

// 调用:
void test() {
    string str = "/media/michael/F/data/UCF-101/UCF-101/ApplyEyeMakeup/v_ApplyEyeMakeup_g01_c01.avi" ;
    string pattern = ".";
    vector<string> result = split(str, pattern);
    cout << "The result:" << endl;
    for(int i=0; i<result.size(); i++) {
        cout << result[i] << endl;
    }
}

 

常用代码库

原文:http://www.cnblogs.com/Michael-Xin/p/4615499.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!