如何标记(单词)将标点符号分类为空格



基于这个问题,这个问题很快就结束了:
试图创建一个程序来读取用户输入,然后将数组分解为单独的单词,我的指针都有效吗?

与其结束,我认为本可以做一些额外的工作来帮助OP澄清这个问题。

问题:

我想标记用户输入,并将标记存储到单词数组中
我想使用标点符号(.,-)作为分隔符,从而将其从令牌流中删除。

在C中,我会使用strtok()将数组分解为令牌,然后手动构建数组
像这样:

主要功能:

char **findwords(char *str);
int main()
{
    int     test;
    char    words[100]; //an array of chars to hold the string given by the user
    char    **word;  //pointer to a list of words
    int     index = 0; //index of the current word we are printing
    char    c;
    cout << "die monster !";
    //a loop to place the charecters that the user put in into the array  
    do
    {
        c = getchar();
        words[index] = c;
    }
    while (words[index] != 'n');
    word = findwords(words);
    while (word[index] != 0) //loop through the list of words until the end of the list
    {
        printf("%sn", word[index]); // while the words are going through the list print them out
        index ++; //move on to the next word
    }
    //free it from the list since it was dynamically allocated
    free(word);
    cin >> test;
    return 0;
}

线路标记器:

char **findwords(char *str)
{
    int     size = 20; //original size of the list 
    char    *newword; //pointer to the new word from strok
    int     index = 0; //our current location in words
    char    **words = (char **)malloc(sizeof(char *) * (size +1)); //this is the actual list of words
    /* Get the initial word, and pass in the original string we want strtok() *
     *   to work on. Here, we are seperating words based on spaces, commas,   *
     *   periods, and dashes. IE, if they are found, a new word is created.   */
    newword = strtok(str, " ,.-");
    while (newword != 0) //create a loop that goes through the string until it gets to the end
    {
        if (index == size)
        {
            //if the string is larger than the array increase the maximum size of the array
            size += 10;
            //resize the array
            char **words = (char **)malloc(sizeof(char *) * (size +1));
        }
        //asign words to its proper value
        words[index] = newword;
        //get the next word in the string
        newword = strtok(0, " ,.-");
        //increment the index to get to the next word
        ++index;
    }
    words[index] = 0;
    return words;
}

如对上述代码有任何意见,将不胜感激
但是,另外,在C++中实现这一目标的最佳技术是什么?

看看boost标记器,它在C++上下文中比strtok()更好。

已经有很多问题涉及如何在C++中标记流
示例:如何在C++中读取文件并获取单词

但更难找到的是如何获得与strtok()相同的功能:

基本上,strtok()允许您将字符串拆分为一整组用户定义的字符,而C++流只允许您使用white space作为分隔符。幸运的是,white space的定义是由区域设置定义的,因此我们可以修改区域设置以将其他字符视为空间,这将允许我们以更自然的方式标记流。

#include <locale>
#include <string>
#include <sstream>
#include <iostream>
// This is my facet that will treat the ,.- as space characters and thus ignore them.
class WordSplitterFacet: public std::ctype<char>
{
    public:
        typedef std::ctype<char>    base;
        typedef base::char_type     char_type;
        WordSplitterFacet(std::locale const& l)
            : base(table)
        {
            std::ctype<char> const&  defaultCType  = std::use_facet<std::ctype<char> >(l);
            // Copy the default value from the provided locale
            static  char data[256];
            for(int loop = 0;loop < 256;++loop) { data[loop] = loop;}
            defaultCType.is(data, data+256, table);
            // Modifications to default to include extra space types.
            table[',']  |= base::space;
            table['.']  |= base::space;
            table['-']  |= base::space;
        }
    private:
        base::mask  table[256];
};

然后我们可以在本地使用这个方面,比如:

    std::ctype<char>*   wordSplitter(new WordSplitterFacet(std::locale()));
    <stream>.imbue(std::locale(std::locale(), wordSplitter));

您问题的下一部分是如何将这些单词存储在数组中。好吧,在C++中你不会。您可以将此功能委托给std::vector/std::string。通过阅读您的代码,您将看到您的代码在代码的同一部分中做两件主要的事情。

  • 它在管理内存
  • 它正在标记数据

有一个基本原则Separation of Concerns,您的代码应该只尝试做两件事中的一件。它应该做资源管理(在这种情况下是内存管理),或者做业务逻辑(数据的标记化)。通过将这些代码分为不同的部分,您可以使代码更易于使用和编写。幸运的是,在这个例子中,所有的资源管理都已经由std::vector/std::string完成了,因此我们可以专注于业务逻辑。

正如已经多次显示的那样,标记流的简单方法是使用运算符>>和字符串。这将使这股洪流变成文字。然后,您可以使用迭代器在流中自动循环,对流进行标记。

std::vector<std::string>  data;
for(std::istream_iterator<std::string> loop(<stream>); loop != std::istream_iterator<std::string>(); ++loop)
{
    // In here loop is an iterator that has tokenized the stream using the
    // operator >> (which for std::string reads one space separated word.
    data.push_back(*loop);
}

如果我们将其与一些标准算法相结合来简化代码。

std::copy(std::istream_iterator<std::string>(<stream>), std::istream_iterator<std::string>(), std::back_inserter(data));

现在将以上所有内容组合到一个应用程序中

int main()
{
    // Create the facet.
    std::ctype<char>*   wordSplitter(new WordSplitterFacet(std::locale()));
    // Here I am using a string stream.
    // But any stream can be used. Note you must imbue a stream before it is used.
    // Otherwise the imbue() will silently fail.
    std::stringstream   teststr;
    teststr.imbue(std::locale(std::locale(), wordSplitter));
    // Now that it is imbued we can use it.
    // If this was a file stream then you could open it here.
    teststr << "This, stri,plop";
    cout << "die monster !";
    std::vector<std::string>    data;
    std::copy(std::istream_iterator<std::string>(teststr), std::istream_iterator<std::string>(), std::back_inserter(data));
    // Copy the array to cout one word per line
    std::copy(data.begin(), data.end(), std::ostream_iterator<std::string>(std::cout, "n"));
}

相关内容

  • 没有找到相关文章

最新更新