我正在尝试制作一个程序,该程序从用户中读取输入文本,然后打印出每个单词,以及按顺序分类的顺序重复多少次。(输入被白色空间隔开,可以容纳标点符号,但没有新的线路)
输入的示例:我是一个好程序员,我喜欢C语言。
输出应为:
-
A:2次
我:1次
AM:1次
好:1次...等等。
**对于输入,我使用了矢量和类,但我不知道为什么我不能在其中推几个项目。
class word {public:
word();
void set_data(string data_value);
void printing_data();
void counter_addition();
int count;
string data;};
int main(){ /*creating vecotr called items of list class type*/
vector<word>items;
/*creating a variable of list class type which is used to push data into the vector*/
word *element;
/*Reading the input text seperated by white spaces*/
int size = 0;
string text;
cout << "Enter the text: " << endl;
getline(cin, text);
size = text.length();
int left_space = -1;
int right_space = 0;
string Sub_String;
/*main loop for constructing substring of words and then manipulating them*/
for (int i = 0; i<size; i++)
{
/*splitting the string into words*/
if (isspace(text[i]) || ispunct(text[i]))
{
right_space = i;
Sub_String = text.substr(left_space + 1, right_space - left_space - 1);
/*for first word just push it*/
if (left_space == -1)
{
element = new word();
element->set_data(Sub_String);
items.push_back(*element);
}
else
{
/*compare to vector's data */
for (int j = 0; j < items.size(); j++)
{
if (Sub_String == items[j].data)
{
items[j].count = items[j].count + 1;
}
else
{
element = new word();
element->set_data(Sub_String);
items.push_back(*element);
}
}
}
left_space = right_space;
}
如果单词与输出相同。
输入:生活生活
输出:重复生命:4
请帮助我,我是编程的新手。
存储字符串值并计数std :: map&lt;std :: string,int>对于每个单词,请检查它是否已经在地图中。如果在那里,请增加计数。在相反的情况下,用计数= 1