基于这个很快关闭的问题:
Trying to create a program to read a users input then break the array into seperate words are my pointers all valid?
而不是关闭我认为一些额外的工作可能会帮助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("%s\n", 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 ++中实现这一目标的最佳技术是什么?
答案 0 :(得分:6)
看一下boost tokenizer在C ++上下文中比strtok()
更好的东西。
答案 1 :(得分:5)
许多问题已经涵盖了如何在C ++中对流进行标记化 示例:How to read a file and get words in 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完成,因此我们可以专注于业务逻辑。
正如已经多次展示的那样,标记化流的简单方法是使用运算符&gt;&gt;和一个字符串。这会将流分解为单词。然后,您可以使用迭代器自动循环流标记流。
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"));
}