【问题标题】:C++ return array of strings from a functionC ++从函数返回字符串数组
【发布时间】:2013-03-25 14:25:04
【问题描述】:

我正在开发一个简单的 CSV 解析器。

从我的 csv 文件中,我得到第一行作为字符串,比如说:

"117;'Tom';'Sawyer';";

我想要实现的是一个将我的字符串分解成碎片的函数,类似于PHP的explode:

$string = "117;'Tom';'Sawyer';";
$row = explode(";", $string);
echo $row[0];

我需要一个在行变量中返回字符串数组的函数。

我是 C++ 新手,所以我不确定要查找或使用什么。

【问题讨论】:

标签: c++ csv explode


【解决方案1】:

这是一个很常见的问题,如果您搜索它,您可以相对轻松地找到它。以下内容可能会有所帮助:

https://stackoverflow.com/a/236803/1974937

【讨论】:

    【解决方案2】:

    您似乎正在寻找一个使用某些指定分隔符拆分字符串并将它们放入顺序容器中的函数。

    这是一个执行此操作的函数:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <iterator>
    
    /// Splits the string using provided delimiters and puts the pieces into a container.
    /// The container must provide push_back and clear methods.
    /// @param a The contaner to put the resulting substrings into
    /// @param str The string to operate on
    /// @param delims Characters that are to be treated as delimiters
    /// @param compress_delims If set to true, will treat mutiple sequential delimiters as a single one
    template<class StringType, class ContainerType>
    void split_string(ContainerType& a, const StringType& str, const StringType& delims, bool compress_delims = true)
    {
            typename StringType::size_type search_from = 0; // Place to start looking for delimiters
            typename StringType::size_type next_delim;      // Location of the next delimiter
    
            a.clear(); // Wipe out previous contents of the output container (it must be empty if the input string is empty)
    
            // Find the first delim after search_from,
            // add the substring between search_from and delimiter location to container,
            // update search_from to delimiter location + 1 so that next time we search,
            // we encounter the next delimiter. Repeat until we find the last delimiter.
            while((next_delim = str.find_first_of(delims, search_from)) != StringType::npos) {
                    // If we encounter multiple delimiters in a row and compress_delims is true
                    // treat it as a single delim.
                    if(!(compress_delims && next_delim - search_from <= 1)){
                            StringType token = str.substr(search_from, next_delim - search_from);
                            a.push_back(token);
                    }
                    search_from = next_delim + 1;
            }
    
            // If we found the last delimiter and there are still some chars after it,
            // just add them to the container.
            if(search_from < str.length())
                    a.push_back(str.substr(search_from));
    }
    
    int main()
    {
            std::vector<std::string> container;
            std::string str = " hello so long    good  bye hurray    ";
            split_string(container, str, std::string(" "));
            std::copy(container.begin(), container.end(), std::ostream_iterator<std::string>(std::cout, ","));
            std::cout <<  " (" << container.size() << ")" << std::endl;
            return 0;
    }
    

    但是,如果可以在您的项目中使用 Boost,我建议您这样做。使用 boost.string_algo library,其中包含用于该特定目的的 split function (example usage)。

    【讨论】:

      猜你喜欢
      • 2013-11-22
      • 2014-11-06
      • 2014-04-11
      • 2013-10-01
      • 2011-05-29
      • 2011-11-23
      相关资源
      最近更新 更多