相同的字符串操作[split]函数 - 对较长数据

时间:2016-11-25 11:27:29

标签: c++ string list for-loop gcc

嗯..我无法比现在的标题更好地描述这个问题,但如果可以的话,请继续。 :)

我正在尝试解析包含数字的1000个字符串。我希望将此字符串从“0”字符中除去,并将剩余的相邻字符放在列表中。例如;

string data =“12304560987598705775068”;

现在,在正常情况下,我希望的目标是将其从0个字符分开并省略零。像下面的数字一样,把它们放在一个列表中: 123 456 9875987 5775 68

下面你可以看到我的代码。它适用于当前的“数据”字符串,但我不能使它在注释中的其他字符串工作。

这是我的编译器信息:

  

gcc --version配置:   --prefix = / Applications / Xcode.app / Contents / Developer / usr --with-gxx-include-dir = / Applications / Xcode.app / Contents / Developer / Platforms / MacOSX.platform / Developer / SDKs / MacOSX10.12 .sdk的/ usr /包括/ C ++ / 4.2.1   Apple LLVM 8.0.0版(clang-800.0.38)目标:   x86_64-apple-darwin16.1.0线程模型:posix InstalledDir:   /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

我在macOS Sierra上使用Netbeans 8.2

这是我的代码:

#include <cstdlib>
#include "iostream"

// following are required for list
#include <algorithm>
#include <list>

using namespace std;

//string data = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
string data = "123045609875987057750687687765554650760078901";

list<string> dS; // dS stands for data set
string particle = ""; // represents each element of dS during runtime

void prepareDataSet(); // gets data set into dS


int main(int argc, char** argv) {

    prepareDataSet();

    return 0;
}

void prepareDataSet(){
    cout << "iteration begins" << endl;
    for(int i=0; i<= sizeof(data); i++){
        if(data.at(i) != '0') {
            particle += data[i];
        }
        else {
            dS.push_back(particle);
            cout << "particle: " << particle << endl;
            particle = "";

            // following deletes from beginning to first 0(zero)

            data.erase(0,(i+1));
            cout << "remaining: " << data << endl;
            cout << "iteration ends" << endl << "************";
            if(sizeof(data)>0) {
                prepareDataSet();
            }
            else {
                break;
            }
        }
    }
}

0 个答案:

没有答案