• c++关于字符串的读入和截取


    #include<iostream>
    #include<string>
    #include<vector>
    using namespace std;
    vector<string> splitEx(const string& src, string separate_character)
    {
    vector<string> strs;

    int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符
    int lastPosition = 0, index = -1;
    while (-1 != (index = src.find(separate_character, lastPosition)))
    {
    strs.push_back(src.substr(lastPosition, index - lastPosition));
    lastPosition = index + separate_characterLen;
    }
    string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容
    if (!lastString.empty())
    strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队
    return strs;
    }


    int main()
    {
    /*
    string s;
    string sum;
    while (cin >> s){
    sum += s + " ";
    if (cin.get() == ' ')
    break;
    }
    cout << sum << endl;
    */
    int tpp = 1;
    string str;
    do{
    string s;
    string temp;
    cout << "输入一行字符串: ";
    while (cin >> s){
    temp += s + " ";
    if (cin.get() == ' ')
    break;
    }
    cout << "继续输入请按1";
    cout << temp << endl;
    str = temp;
    cin >> tpp;

    } while (tpp==1);


    //string str;
    /*
    int count;//单词个数
    int i, j;
    */

    //cout << "输入一行字符串: ";
    //getline(cin, str);

    /*
    for (count = 0, j = str.size(), i = 0; i<j;)
    {
    while (str[i] == ' '&&i<j) i++;
    if (i<j) count++;
    while (str[i] != ' '&&i<j) i++;
    }
    cout << "j=" << str.size() << endl;
    cout << "输入的字符串为: " << str << endl;
    cout << "字符串中包含的单词数为:" << count << endl;
    */

    string del = " ";
    vector<string> strs = splitEx(str, del);
    //cout << "strsSize=" << strs.size() << endl;
    cout << "输入的字符串为: " << str << endl;
    cout << "字符串中包含的单词数为:" << strs.size() << endl;
    //int a,b,c,d;
    unsigned int i = strs.size()-1;
    while (i >=0)
    //for (unsigned int i = 0; i < strs.size(); i++)
    {
    int j = 1;
    int a = atoi(strs[i].c_str());
    int b = atoi(strs[i-1].c_str());
    int c = atoi(strs[i - 2].c_str());
    int d = atoi(strs[i-3].c_str());
    int cc = atoi(strs[j+2].c_str());
    //cout <<b+1 << endl;
    if (a == b && c == d){
    cout << "yes" << endl;
    break;
    }

    else
    {
    cout << "no" << endl;
    break;
    }
    //i -= 3;
    }


    cout << endl;
    system("pause");
    return 0;


    }

  • 相关阅读:
    KMP算法(字符串匹配)
    C 语言结构体之点运算符( . )和箭头运算符( -> )的区别
    归并排序(分治法)
    插入排序(挖坑)
    快速排序(挖坑+分治法)
    C++--------------------------------指针和数组替换使用原因
    广度优先搜索(BFS)----------------(TjuOj1140_Dungeon Master)
    图的最短路径-----------SPFA算法详解(TjuOj2831_Wormholes)
    最小生成树问题------------Prim算法(TjuOj_1924_Jungle Roads)
    图的最短路径-----------Dijkstra算法详解(TjuOj2870_The Kth City)
  • 原文地址:https://www.cnblogs.com/beihaidao/p/8540874.html
Copyright © 2020-2023  润新知