码迷,mamicode.com
首页 > 其他好文 > 详细

常用代码库

时间:2015-07-02 11:58:09      阅读:158      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!