簡體   English   中英

用不同的分隔符拆分字符串

[英]Splitting a string with different delimiters

具有以下類型的輸入:

add name, breed, birthDate, vaccinationsCount, photograph

(例如add boo, yorkshire terrier, 01-13-2017, 7, boo puppy.jpg )

我想拆分此字符串以從中獲取我的參數,但它不起作用。

我的代碼如下所示:

getline(cin, listOfCommands);
string functionToApply = listOfCommands.substr(0, listOfCommands.find(" "));
int position = listOfCommands.find(" ");
listOfCommands.erase(0, position + 1);
cout << listOfCommands;
if (functionToApply == "exit")
    break;
else if (functionToApply == "add")
{
    position = listOfCommands.find(", ");
    string name = listOfCommands.substr(0, position);
    listOfCommands.erase(0, position + 1);
    position = listOfCommands.find(", ");
    string breed = listOfCommands.substr(0, position);
    listOfCommands.erase(0, position + 2);
    position = listOfCommands.find(", ");
    string birthDate = listOfCommands.substr(0, position);
    listOfCommands.erase(0, position + 2);
    position = listOfCommands.find(", ");
    string nrShorts = listOfCommands.substr(0, position);
    listOfCommands.erase(0, position + 2);
    string photo = listOfCommands;
}

有人能幫助我嗎?

對於此示例我使用 的std ::函數getline與自定義分隔符,標准::字符串流,以幫助解析在輸入的流的std :: vector的存儲參數,(如果你願意,你可以將它們分配給你為他們創建的變量):

現場樣品

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

int main() {

    std::string listOfCommands, temp, command;
    std::vector<std::string> args; //container for the arguments

    //retrieve command
    getline(std::cin, command, ' ');

    if (command == "add") {

        getline(std::cin, listOfCommands);     
        std::stringstream ss(listOfCommands);

        while (getline(ss, temp, ',')) { //parse comma separated arguments

            while (*(temp.begin()) == ' ')
                temp.erase(temp.begin()); //remove leading blankspaces

            args.push_back(temp); // add parameter to container
        }

        //test print
        for (std::string str : args){
            std::cout << str << std::endl;
        }
    }
    return 0;
}

輸入:

add boo, yorkshire terrier, 01-13-2017, 7,    boo puppy.jpg

輸出:

boo
yorkshire terrier
01-13-2017
7
boo puppy.jpg

嘗試regex_token_iterator

#include <regex>

const int split_constant = -1;
std::vector<std::string> args(
  std::sregex_token_iterator(listOfCommands.begin(), 
                             listOfCommands.end(), 
                             std::regex(", "), 
                             split_constant),
  std::sregex_token_iterator());

當然,您不必將標記保存在向量中,您也可以對它們進行迭代:

auto iter = std::sregex_token_iterator(listOfCommands.begin(), 
                                       listOfCommands.end(), 
                                       std::regex(", "), 
                                       split_constant);
const string functionToApply = *iter++;
if (functionToApply == "exit") break;
const string name  = *iter++;
const string breed = *iter++;
// etc.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM