匯東網


文本 / 字符串快速分析模版代码(示例:提取英文单词)

[編輯] [转简体]
|
作者:huidong | 分類:【編程】C/C++
[ 16 瀏覽 0 評論 2 贊 5 踩 ]

概要

正文

#include <stdio.h>
#include <conio.h>
#include <string>
#include <vector>
#include <fstream>
#include <streambuf>
using namespace std;

// 获取文件内容
string GetFile(const char* filepath)
{
    ifstream t(filepath);
    string str((istreambuf_iterator<char>(t)), istreambuf_iterator<char>());
    t.close();

    if (str.size() <= 0)
    {
        printf("\n\nopen %s error.", filepath);
        _getch();
        exit(-1);
    }

    return str;
}

// 是否为英文字母(或空格)
bool isEnglishAlphabet(char c)
{
    return (c >= 65 && c <= 90) || (c >= 97 && c <= 122);
}

vector<string> Load(string res)
{
    vector<string> list;
    string name;
    for (int i = 0; i < (int)res.size(); i++)
    {
        if (isEnglishAlphabet(res[i]))
        {
            name += res[i];
        }
        else if (name.size() > 0)
        {
            list.push_back(name);
            name = "";
        }
    }
    return list;
}

int main()
{
    vector<string> list = Load(GetFile("t.txt"));
    
    for (int i = 0; i < (int)list.size(); i++)
    {
        printf("%s\n", list[i].c_str());
    }
    _getch();

    return 0;
}


[ 2] [ 5]


 評論區  0 條評論

+ 添加評論