【问题标题】:have a programming project for an intro c++ class one of the function we need to create is a split function有一个介绍 c++ 类的编程项目我们需要创建的函数之一是拆分函数
【发布时间】:2016-02-29 23:25:51
【问题描述】:

我希望得到一些反馈,说明我是否以“智能方式”执行此操作,或者我是否可以更快地执行此操作。如果我在空白处分裂 我可能会使用 getline(stringstream, word, delimiter) 但我不知道如何使定界符适应所有好字符,所以我只是循环整个字符串生成一个新单词,直到我遇到一个坏字符,但由于我对编程相当陌生,我不确定它是否是最好的方法它 感谢您的任何反馈

            #include <iostream>
            #include <string> 
            using std::string;
            #include <vector> 
            using std::vector;
            #include <sstream>
            #include <algorithm>
            #include <iterator> //delete l8r

            using std::cout; using std::cin; using std::endl;
            /*
            void split(string line, vector<string>&words, string good_chars)
            o
            Find words in the line that consist of good_chars. 
            Any other character is considered a separator. 
            o
            Once you have a word, convert all the characters to lower case.

            You then push each word onto the reference vector words.
            Important: split goes  in  its  own  file.  This  is  both  for  your  own  benefit,  you  can  reuse 
            split, and for grading purposes.We will provide a split.h for you.
            */
            void split(string line, vector<string> & words, string good_chars){
                string good_word;
                for(auto c : line){
                    if(good_chars.find(c)!=string::npos){
                        good_word.push_back(c);
                    }
                    else{
                        if(good_word.size()){
                            std::transform(good_word.begin(), good_word.end(), good_word.begin(), ::tolower);
                            words.push_back(good_word);
                        }
                        good_word = "";
                    }
                }
            }
            int main(){
                vector<string> words;
                string good_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'";
                // TEST split
                split("This isn't a TEST.", words, good_chars);
                // words should have: {"this", "isn't", "a", "test"}, no period in test
                std::copy(words.begin(), words.end(), std::ostream_iterator<string>(cout, ","));
                cout << endl;
                return 0;
            }

【问题讨论】:

    标签: c++ function split


    【解决方案1】:

    鉴于 C++ 类介绍的上下文,我想说这是一种合理的方法。我什至会说,这很可能是您的讲师希望看到的方法。

    当然,可以进行一些优化调整。就像实例化一个包含 256 个元素的 bool 数组,使用 good_chars 将相应的值设置为 true,而所有其他默认值都设置为 false,然后将 find() 调用替换为快速数组查找。

    但是,我预想如果你要交这样的东西,你会被怀疑复制了你在中间管上找到的东西,所以别管它。

    您可能会考虑做的一件事是在您 push_back 每个字符时使用 tolower,并删除多余的 std::transform 传递单词。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 2014-03-12
      • 2022-01-20
      • 2022-01-23
      • 2012-10-11
      • 2018-08-30
      • 1970-01-01
      相关资源
      最近更新 更多