在数组中搜索以查找单词的单独实例 这是我的高级C++类中的一个作业,我们正在研究指针和动态分配的数组,虽然这似乎不是我的问题所在……/P>
函数头、引号、单词和计数是重要的数组,NumWords只包含引号数组中有多少元素的计数在数组中搜索以查找单词的单独实例 这是我的高级C++类中的一个作业,我们正在研究指针和动态分配的数组,虽然这似乎不是我的问题所在……/P>,c++,search,dynamic-arrays,C++,Search,Dynamic Arrays,函数头、引号、单词和计数是重要的数组,NumWords只包含引号数组中有多少元素的计数 void CountWords ( char ** Quote, char **& Words, int *& Count, int NumWords ) { 变量等,一些数组通过指针从main传递 char ** Temp = nullptr; int * ITemp = nullptr; int WordCount = 0; int QuoteCount
void CountWords ( char ** Quote, char **& Words, int *& Count, int NumWords )
{
变量等,一些数组通过指针从main传递
char ** Temp = nullptr;
int * ITemp = nullptr;
int WordCount = 0;
int QuoteCount = 0;
接下来是for循环的启动读数。我正在创建两个动态分配的数组,一个用于存储在引号数组中新找到的单词实例,另一个用于存储该单词在引号数组中出现的次数计数。所有这些看起来都很好,但我不喜欢所有东西都有多大(代码膨胀),任何让它以不同方式开始的建议都是非常好的
字符的第一个动态数组
Temp = new char * [WordCount + 1];
for (int z = 0; z < WordCount; z++)
{
Temp[z] = Words[z];
}
Temp[WordCount] = new char[ strlen( Quote[WordCount]) +1 ];
strcpy( Temp[WordCount], Quote[WordCount] );
delete [] Words;
Words = Temp;
这就是事情开始变得不稳定的地方
for (int i = 0; i < NumWords; i++)
{
//Right here is where the program breaks, after the second iteration of this
//For-loop. What happens is the loop counter (i) increments to 2 shortly
//before Words[2] gets created. I've tried decrementing i inside the If,
//where QuoteCount gets incremented, but that causes an out-of-bounds error
//on the Quote array on the last iteration.
//Check to see if Quote value is in Words
if (_stricmp( Quote[ QuoteCount ], Words[ i ]) == 0 )
{
//If true, move on to next element in Quote array.
QuoteCount++;
}
//If false, write Quote value to Words
else
{
Temp = new char * [WordCount + 1];
for (int z = 0; z < WordCount; z++)
{
Temp[z] = Words[z];
}
Temp[WordCount] = new char[ strlen( Quote[WordCount]) +1 ];
strcpy( Temp[WordCount], Quote[WordCount] );
delete [] Words;
Words = Temp;
//Create new array element in Count to track new value in Words
ITemp = new int [ WordCount + 1 ];
for (int z = 0; z < WordCount; z++)
{
ITemp[z] = Count[z];
}
ITemp[WordCount] = 0;
delete [] Count;
Count = ITemp;
//Check Quote for other instances of Word
for (int j = 0; j < NumWords; j++)
{
if (_stricmp( Words[ WordCount ], Quote[j]) == 0 )
{
Count[ WordCount ]++;
}//if
}//for
//Increment word counter, indicating a new word was stored.
WordCount++;
}//else
}//for
}//function
for(int i=0;i
我觉得这已经变得比需要的复杂得多了。我开始尝试嵌套循环,但似乎也无法实现。另一件事是,一旦它将一个单词从引语复制到另一个单词,它就不应该再复制那个单词
此外,一般输入的代码质量和这样的将是赞赏的。我正努力在以后的生活中成为一名软件工程师,我喜欢在我所做的事情上做得最好,所以我需要一直尽可能多地学习。你的主要问题是你似乎想用
I
逐步完成引用,但同时你也在用它来索引单词。一种更合乎逻辑的方法是,对照已复制到单词中的现有单词检查引号中的每个单词,并相应地增加计数或插入单词。您还可以利用单词中最多有NumWords
这一事实,并在函数开始时分配足够的内存:
void CountWords(char const** Quote, char**& Words, int*& Count, int NumWords)
{
Words = new char*[NumWords];
Count = new int[NumWords];
int words = 0;
for (int i = 0; i < NumWords; ++i) {
int j = 0;
for (; j < words; ++j) {
if (!strcmp(Quote[i], Words[j])) { // Duplicate word
++Count[j];
break;
}
}
if (j == words) { // New word found
Words[words] = new char[strlen(Quote[i]) + 1]{};
strcpy(Words[words], Quote[i]);
Count[words] = 1;
words++;
}
}
Count[words] = 0;
}
int main()
{
char const* quote[] = {"Hello", "world", "hello", "world"};
char** words;
int* count;
CountWords(quote, words, count, 4);
for (int i = 0; count[i]; ++i) {
std::cout << words[i] << ' ' << count[i] << '\n';
delete[] words[i];
}
delete[] words;
delete[] count;
}
这样使用:app
是否禁止您使用标准容器,如string
和vector
?否vectors
,我们还没有找到它们。至于string
你的意思是cstring
?我想不是,这一章中也介绍了strstrstr
、strtok
和strchr
函数。不,我是说std::string
,但现在看来你做事情很难。然后就是很难了。在学习酷的东西之前必须先学习基础知识,对吗?如果我能解决递增的问题,我想剩下的部分应该可以解决。除非递增问题是我看不到的潜在问题的症状。你能提供函数参数示例吗?这似乎工作得很好。我必须在开始时为循环
添加一个,以便将0写入新计数数组的所有元素。除此之外,太棒了!非常感谢你。我正要把头发拔出来…@Teravian好主意!去证明这东西是多么容易出错。另一个选项是更改Count[words]++代码>到<代码>计数[字]=1代码>取而代之。您还可以按如下方式设置数组的零:Count=newint[NumWords]{}代码>
WordCount++;
QuoteCount++;
for (int i = 0; i < NumWords; i++)
{
//Right here is where the program breaks, after the second iteration of this
//For-loop. What happens is the loop counter (i) increments to 2 shortly
//before Words[2] gets created. I've tried decrementing i inside the If,
//where QuoteCount gets incremented, but that causes an out-of-bounds error
//on the Quote array on the last iteration.
//Check to see if Quote value is in Words
if (_stricmp( Quote[ QuoteCount ], Words[ i ]) == 0 )
{
//If true, move on to next element in Quote array.
QuoteCount++;
}
//If false, write Quote value to Words
else
{
Temp = new char * [WordCount + 1];
for (int z = 0; z < WordCount; z++)
{
Temp[z] = Words[z];
}
Temp[WordCount] = new char[ strlen( Quote[WordCount]) +1 ];
strcpy( Temp[WordCount], Quote[WordCount] );
delete [] Words;
Words = Temp;
//Create new array element in Count to track new value in Words
ITemp = new int [ WordCount + 1 ];
for (int z = 0; z < WordCount; z++)
{
ITemp[z] = Count[z];
}
ITemp[WordCount] = 0;
delete [] Count;
Count = ITemp;
//Check Quote for other instances of Word
for (int j = 0; j < NumWords; j++)
{
if (_stricmp( Words[ WordCount ], Quote[j]) == 0 )
{
Count[ WordCount ]++;
}//if
}//for
//Increment word counter, indicating a new word was stored.
WordCount++;
}//else
}//for
}//function
void CountWords(char const** Quote, char**& Words, int*& Count, int NumWords)
{
Words = new char*[NumWords];
Count = new int[NumWords];
int words = 0;
for (int i = 0; i < NumWords; ++i) {
int j = 0;
for (; j < words; ++j) {
if (!strcmp(Quote[i], Words[j])) { // Duplicate word
++Count[j];
break;
}
}
if (j == words) { // New word found
Words[words] = new char[strlen(Quote[i]) + 1]{};
strcpy(Words[words], Quote[i]);
Count[words] = 1;
words++;
}
}
Count[words] = 0;
}
int main()
{
char const* quote[] = {"Hello", "world", "hello", "world"};
char** words;
int* count;
CountWords(quote, words, count, 4);
for (int i = 0; count[i]; ++i) {
std::cout << words[i] << ' ' << count[i] << '\n';
delete[] words[i];
}
delete[] words;
delete[] count;
}
#include <iostream>
#include <unordered_map>
int main()
{
std::string word;
std::unordered_map<std::string, int> map;
while (std::cin >> word)
++map[word];
for (auto const& w : map)
std::cout << w.first << " : " << w.second << '\n';
}