将字符串拆分并转换为字符数组

3

如何转换:

 string x = "1+2+3";

to:

 char y[] = {'1', '2', '3'};

我应该采取什么方法?


由于您仅接受一位数,因此简单的for循环就应该足够了。到目前为止,您尝试解决的问题有哪些? - Quimby
使用std :: getline函数对字符串进行操作,并将'+'作为分隔符传递。 - N. Prone
4个回答

5
任务是按照“+”分隔字符串。在下面的示例中,使用分隔符“,”。
将字符串拆分为标记是一项非常古老的任务。有许多解决方案可用。它们都有不同的属性。有些难以理解,有些难以开发,有些更加复杂、慢或灵活或不灵活。
备选方案:
  1. 手工制作,有许多变种,使用指针或迭代器,可能难以开发并容易出错。
  2. 使用旧式的 std::strtok 函数。可能不安全。也许不应再使用
  3. std::getline。最常用的实现。但实际上这是一个“误用”,并不太灵活
  4. 使用专门为此目的开发的现代函数,最灵活,并且适合于STL环境和算法景观。但是速度较慢。
请参见以下代码中的4个示例。
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <regex>
#include <algorithm>
#include <iterator>
#include <cstring>
#include <forward_list>
#include <deque>

using Container = std::vector<std::string>;
std::regex delimiter{ "," };


int main() {

    // Some function to print the contents of an STL container
    auto print = [](const auto& container) -> void { std::copy(container.begin(), container.end(),
        std::ostream_iterator<std::decay<decltype(*container.begin())>::type>(std::cout, " ")); std::cout << '\n'; };

    // Example 1:   Handcrafted -------------------------------------------------------------------------
    {
        // Our string that we want to split
        std::string stringToSplit{ "aaa,bbb,ccc,ddd" };
        Container c{};

        // Search for comma, then take the part and add to the result
        for (size_t i{ 0U }, startpos{ 0U }; i <= stringToSplit.size(); ++i) {

            // So, if there is a comma or the end of the string
            if ((stringToSplit[i] == ',') || (i == (stringToSplit.size()))) {

                // Copy substring
                c.push_back(stringToSplit.substr(startpos, i - startpos));
                startpos = i + 1;
            }
        }
        print(c);
    }

    // Example 2:   Using very old strtok function ----------------------------------------------------------
    {
        // Our string that we want to split
        std::string stringToSplit{ "aaa,bbb,ccc,ddd" };
        Container c{};

        // Split string into parts in a simple for loop
#pragma warning(suppress : 4996)
        for (char* token = std::strtok(const_cast<char*>(stringToSplit.data()), ","); token != nullptr; token = std::strtok(nullptr, ",")) {
            c.push_back(token);
        }

        print(c);
    }

    // Example 3:   Very often used std::getline with additional istringstream ------------------------------------------------
    {
        // Our string that we want to split
        std::string stringToSplit{ "aaa,bbb,ccc,ddd" };
        Container c{};

        // Put string in an std::istringstream
        std::istringstream iss{ stringToSplit };

        // Extract string parts in simple for loop
        for (std::string part{}; std::getline(iss, part, ','); c.push_back(part))
            ;

        print(c);
    }

    // Example 4:   Most flexible iterator solution  ------------------------------------------------

    {
        // Our string that we want to split
        std::string stringToSplit{ "aaa,bbb,ccc,ddd" };


        Container c(std::sregex_token_iterator(stringToSplit.begin(), stringToSplit.end(), delimiter, -1), {});
        //
        // Everything done already with range constructor. No additional code needed.
        //

        print(c);


        // Works also with other containers in the same way
        std::forward_list<std::string> c2(std::sregex_token_iterator(stringToSplit.begin(), stringToSplit.end(), delimiter, -1), {});

        print(c2);

        // And works with algorithms
        std::deque<std::string> c3{};
        std::copy(std::sregex_token_iterator(stringToSplit.begin(), stringToSplit.end(), delimiter, -1), {}, std::back_inserter(c3));

        print(c3);
    }
    return 0;
}

1
你可以使用 std::vector<std::string> 替代 char[],这样可以处理多位数字。尝试以下代码:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

int main() {
    using namespace std;
    std::string str("1+2+3");
    std::string buff;
    std::stringstream ss(str);
    
    std::vector<std::string> result;
    while(getline(ss, buff, '+')){
        result.push_back(buff);
    }
    
    for(std::string num : result){
        std::cout << num << std::endl;
    }
}

这里是一个coliru链接,展示了它可以处理多位数字。

0

你可以在 https://repl.it/@JomaCorpFX/StringSplit#main.cpp 运行/检查这段代码

代码

#include <iostream>
#include <vector>

std::vector<std::string> Split(const std::string &data, const std::string &toFind)
{
    std::vector<std::string> v;
    if (data.empty() || toFind.empty())
    {
        v.push_back(data);
        return v;
    }
    size_t ini = 0;
    size_t pos;
    while ((pos = data.find(toFind, ini)) != std::string::npos)
    {
        std::string s = data.substr(ini, pos - ini);
        if (!s.empty())
        {
            v.push_back(s);
        }
        ini = pos + toFind.length();
    }
    if (ini < data.length())
    {
        v.push_back(data.substr(ini));
    }

    return v;
}

int main()
{
    std::string x = "1+2+3";
    for (auto value : Split(x, u8"+"))
    {
        std::cout << "Value: " << value << std::endl;
    }
    std::cout << u8"Press enter to continue... ";
    std::cin.get();
    return EXIT_SUCCESS;
}

输出

Value: 1
Value: 2
Value: 3
Press enter to continue...

clang++


0

以下是我的步骤:

  • 将原始的string转换为char*
  • 使用函数strtok将获得的char*按照分隔符+进行拆分。我将每个标记存储到一个vector<char>
  • 将这个vector<char>转换为C字符数组char*
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

int main()
{
    string line = "1+2+3";
    std::vector<char> vectChar;
    // convert the original string into a char array to allow splitting
    char* input= (char*) malloc(sizeof(char)*line.size());
    strcpy(input,line.data());
    // splitting the string 
    char *token = strtok(input, "+");

    int len=0;
    while(token) {
        std::cout << *token;
        vectChar.push_back(*token);
        token = strtok(NULL, "+");
    }
    // end of splitting step

    std::cout << std::endl;
    //test display the content of the vect<char>={'1', '2', ...}
    for (int i=0; i< vectChar.size(); i++)
    {
        std::cout << vectChar[i];
    }
    // Now that the vector contains the needed list of char
    // we need to convert it to char array (char*)
    // first malloc
    char* buffer = (char*) malloc(vectChar.size()*sizeof(char));
    // then convert the vector into char*
    std::copy(vectChar.begin(), vectChar.end(), buffer);
    std::cout << std::endl;
    //now buffer={'1', '2', ...}
    // les ut stest by displaying
    while ( *buffer != '\0')
    {
        printf("%c", *buffer);
        buffer++;
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接