C ++字符串帮助,吐出一个字符串

时间:2017-05-19 01:14:11

标签: c++ string c++11 vector split

我正在编写一个程序,假设能够打开文件,读取每一行并分隔最后一个名字,名字:3个测试分数。正在阅读的文件的格式是Mark Titan:80 80 85.我想输出TITAN,Mark:80 80 85.我们正在使用字符串,到目前为止使用我的老师代码我已经完全分开了文件。它将按1-100的顺序显示测试分数(100首先是因为它从1开始,但我可以在之后修复),然后按字母顺序显示名称。我需要帮助创建该行的substr,创建一个只有全名的字符串,然后将其拆分为first和last,然后对它们进行正确排序。我一直在搞乱.find,但我不知道如何将这个矢量分成更小的矢量。请帮忙,谢谢你。

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <sstream>

    using namespace std;
    void openFile(ifstream &in);
    void processFile(ifstream &in, vector<string> &list);
    void display(const vector<string> &list);
    void sort(vector<string> &list);

    int main(int argc, char *argv[])
    {
        ifstream in;
        string line;
        vector<string> words;
        openFile(in);
        processFile(in, words);
        display(words);
        return 0;
    }


    void openFile(ifstream &in)
    {
        string fileName;
        bool again;
        do
        {
            again = false;
            cout<<"What is the name of the file to you wish to sort? (.txt will be added if no extension is included): ";
            cin>>fileName;
            if(fileName.find('.') >= fileName.size() )
                fileName +=  ".txt";
            in.open(fileName.c_str());
            if(in.fail())
            {
                in.close();
                in.clear();
                again = true;
                cout<<"That file does not exist! Please re-enter"<<endl;
            }
        }while(again);
    }

    void processFile(ifstream &in, vector<string> &list)
    {
        string line, word;
        int s1,s2,s3;
        stringstream ss;
        while(getline(in, line, ':'))
        {
            ss<<line.substr(line.find(' ') + 1);
            while(ss>>word)
                list.push_back(word);
            ss.clear();
        }
        sort(list);
    }

    void sort(vector<string> &list)
    {
        for(unsigned int i =  0; i < list.size(); ++i)
            for(unsigned int j = 0; j < list.size(); ++j)
                if(list[i] < list[j])
                {
                    string temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                }
    }

    void display(const vector<string> &list)
    {
        cout<<"The file read had "<<list.size()<<" words in it"
            <<endl<<"In sorted order, they are:"<<endl;
        for(unsigned int i = 0; i < list.size();++i)
            cout<<list[i]<<endl;}

2 个答案:

答案 0 :(得分:0)

您可以对该行进行两次标记。首先,拆分冒号,然后拆分空格上的第一个标记。此外,你的老师会向你扔空格和畸形的线条。例如,缺少冒号或缺少名称(或缺少姓/姓)。通过计算拆分字符串或子字符串时返回的标记来处理这些错误。

void Tokenize(const std::string& theSourceString, std::vector<std::string>& theTokens, const std::string& theDelimiter)
{
    // If no delimiter is passed, tokenize on all white space.
    if (theDelimiter.empty())
    {
        std::string aBuffer; // Have a buffer string
        std::stringstream ss(theSourceString); // Insert the string into a stream

        while (ss >> aBuffer)
        {
            theTokens.push_back(aBuffer);
        }
        return; //?
    }

    // Skip delimiters at beginning.
    std::string::size_type aLastPosition = theSourceString.find_first_not_of(theDelimiter, 0);

    // Find first "non-delimiter".
    std::string::size_type aPosition = theSourceString.find_first_of(theDelimiter, aLastPosition);

    while (aPosition != std::string::npos || aLastPosition != std::string::npos)
    {
        // Found a token, add it to the vector.
        theTokens.push_back(theSourceString.substr(aLastPosition, aPosition - aLastPosition));

        // Skip delimiters.  Note the "not_of"
        aLastPosition = theSourceString.find_first_not_of(theDelimiter, aPosition);

        // Find next "non-delimiter"
        aPosition = theSourceString.find_first_of(theDelimiter, aLastPosition);
    }
}

使用示例:

std::vector<std::string> tokens;
Tokenize("{ 1, 2, 3, 4, 5 }", tokens, "{}, " );

答案 1 :(得分:0)

我有这个Utility类,它有一堆字符串操作方法。我将展示用于使用分隔符拆分字符串的类函数。此类具有私有构造函数,因此您无法创建此类的实例。所有方法都是静态方法。

<强> Utility.h

#ifndef UTILITY_H
#define UTILITY_h

// Library Includes Here: vector, string etc.

class Utility {
public:
    static std::vector<std::string> splitString( const std::string& strStringToSplit,
                                                 const std::string& strDelimiter,
                                                 const bool keepEmpty = true );

private:
    Utility();
};

<强> Utility.cpp

std::vector<std::string> Utility::splitString( const std::string& strStringToSplit, 
                                               const std::string& strDelimiter, 
                                               const bool keepEmpty ) {
    std::vector<std::string> vResult;
    if ( strDelimiter.empty() ) {
        vResult.push_back( strStringToSplit );
        return vResult;
    }

    std::string::const_iterator itSubStrStart = strStringToSplit.begin(), itSubStrEnd;
    while ( true ) {
        itSubStrEnd = search( itSubStrStart, strStringToSplit.end(), strDelimiter.begin(), strDelimiter.end() );
        std::string strTemp( itSubStrStart, itSubStrEnd );
        if ( keepEmpty || !strTemp.empty() ) {
            vResult.push_back( strTemp );
        }

        if ( itSubStrEnd == strStringToSplit.end() ) {
            break;
        }

        itSubStrStart = itSubStrEnd + strDelimiter.size();
    }

    return vResult;    
} 

Main.cpp - 用法

#include <string>
#include <vector>
#include "Utility.h"

int main() {
    std::string myString( "Hello World How Are You Today" );

    std::vector<std::string> vStrings = Utility::splitString( myString, " " );

    // Check Vector Of Strings
    for ( unsigned n = 0; n < vStrings.size(); ++n ) {
        std::cout << vStrings[n] << " ";
    }
    std::cout << std::endl;

    // The Delimiter is also not restricted to just a single character
    std::string myString2( "Hello, World, How, Are, You, Today" );

    // Clear Out Vector
    vStrings.clear();

    vStrings = Utility::splitString( myString2, ", " ); // Delimiter = Comma & Space

    // Test Vector Again
    for ( unsigned n = 0; n < vStrings.size(); ++n ) {
        std::cout << vStrings[n] << " ";
    }
    std::cout << std::endl;

    return 0;
}