C++ 在for循环之后,字符串数据似乎被删除
在下面的代码中,每当我转到调试器时,段落的值都会被删除,或者返回到0,我似乎不明白为什么,想一想C++ 在for循环之后,字符串数据似乎被删除,c++,arrays,string,data-loss,C++,Arrays,String,Data Loss,在下面的代码中,每当我转到调试器时,段落的值都会被删除,或者返回到0,我似乎不明白为什么,想一想 void getFreqLetter(string paragraph){ char alphabet[26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; i
void getFreqLetter(string paragraph){
char alphabet[26] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
int counter[26];
//set counter values to zero
for (int clear = 0; clear < sizeof(counter) - 1; ++clear){
counter[clear] = 0;
}
cout << paragraph;
int result = 0;
for (int i = 0; i < sizeof(paragraph); ++i){
//case:found
for (int j = 0; j < sizeof(alphabet) - 1; ++j){
if (alphabet[j] == paragraph[i]){
counter[j]++;
}
}
//go through array find largest value
for (int k = 0; k < sizeof(counter) - 1; ++k){
if (counter[k] > result){ result = counter[k]; }
}
cout << result;
}
}
void getFreqLetter(字符串段落){
字符字母[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
整数计数器[26];
//将计数器值设置为零
对于(int clear=0;clear cout总之,所有问题都是由于您误用了sizeof
sizeof(段落)
并不是在做您认为它在做的事情:它返回的是string
类的大小,而不是string实例中的字符数
您应该使用paragration.size()
而不是假设它是std::string
类型
sizeof(alphabet)
通过幸运的巧合返回数组中的元素数:sizeof(char)
被标准定义为1。这样的“ace”代码应该附有注释
您使用sizeof(counter)
就没那么幸运了。您得到的值是sizeof(int)
的倍数,它随平台的不同而变化(2、4和8是常见的)。您应该编写sizeof(counter)/sizeof(int)
或sizeof(counter)/sizeof(counter[0])
。后者是一些人喜欢的,因为您不需要对该类型进行硬编码,并且由于C++标准不允许零长度数组,所以<代码>计数器[0 ] < /C>定义良好。
(这里需要记住的是,sizeof
是在编译时计算的)。总之,所有问题都是由于您误用了sizeof
sizeof(段落)
并不是在做您认为它在做的事情:它返回的是string
类的大小,而不是string实例中的字符数
您应该使用paragration.size()
而不是假设它是std::string
类型
sizeof(alphabet)
通过幸运的巧合返回数组中的元素数:sizeof(char)
被标准定义为1。这样的“ace”代码应该附有注释
您使用sizeof(counter)
就没那么幸运了。您得到的值是sizeof(int)
的倍数,它随平台的不同而变化(2、4和8是常见的)。您应该编写sizeof(counter)/sizeof(int)
或sizeof(counter)/sizeof(counter[0])
。后者是一些人喜欢的,因为您不需要对该类型进行硬编码,并且由于C++标准不允许零长度数组,所以<代码>计数器[0 ] < /C>定义良好。
(这里需要记住的是,sizeof
是在编译时计算的)。很简单。循环条件中的sizeof(counter)
实际上是sizeof(int)*26
,因此循环(从迭代27开始)会压碎字母表,堆栈中的一些也会压碎字母表(特别是将返回地址设置为0,以及段落
参数的内部)
您的-1
不应该在那里,因为您使用的是严格的比较
您可能希望跟踪第一个循环并观察sizeof(counter)
的值
如果需要计数器
数组中的元素数,一种惯用的方法是sizeof(counter)/sizeof(counter[0])
此外,字符串的长度应通过段落.size()
获得,因为sizeof(段落)
返回管理字符串的对象的大小,而不是字符串本身
最后,sizeof(alphabet)
做了正确的事情,因为sizeof(char)
被定义为1
现在,对于一些C++11魔法:
#include <array>
const std::array<char,26u> alphabet{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
std::array<int, 26u> counter;
//set counter values to zero
for (int clear = 0; clear < counter.size(); ++clear){
counter[clear] = 0;
}
#包括
常量std::数组字母{a',b',c',d',e',f',g',h',
‘i’、‘j’、‘k’、‘l’、‘m’、‘n’、‘o’、‘p’、‘q’、‘r’,
‘s’、‘t’、‘u’、‘v’、‘w’、‘x’、‘y’、‘z’};
阵列计数器;
//将计数器值设置为零
对于(int clear=0;clear
这将消除许多(所有)的大小陷阱,同时保持同样的效率。简单。循环条件中的sizeof(counter)
实际上是sizeof(int)*26
,因此循环(从迭代27开始)会压碎字母表,堆栈中的一些也会压碎字母表(特别是将返回地址设置为0,以及段落
参数的内部)
您的-1
不应该在那里,因为您使用的是严格的比较
您可能希望跟踪第一个循环并观察sizeof(counter)
的值
如果需要计数器
数组中的元素数,一种惯用的方法是sizeof(counter)/sizeof(counter[0])
此外,字符串的长度应通过段落.size()
获得,因为sizeof(段落)
返回管理字符串的对象的大小,而不是字符串本身
最后,sizeof(alphabet)
做了正确的事情,因为sizeof(char)
被定义为1
现在,对于一些C++11魔法:
#include <array>
const std::array<char,26u> alphabet{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
std::array<int, 26u> counter;
//set counter values to zero
for (int clear = 0; clear < counter.size(); ++clear){
counter[clear] = 0;
}
#包括
常量std::数组字母{a',b',c',d',e',f',g',h',
‘i’、‘j’、‘k’、‘l’、‘m’、‘n’、‘o’、‘p’、‘q’、‘r’,
‘s’、‘t’、‘u’、‘v’、‘w’、‘x’、‘y’、‘z’};
阵列计数器;
//将计数器值设置为零
对于(int clear=0;clear
这将删除许多(所有)的sizeof
陷阱,
for (int clear = 0; clear < sizeof( counter ) / sizeof( *counter ); ++clear){
counter[clear] = 0;
}
for (int clear = 0; clear < 26; ++clear){
counter[clear] = 0;
}
std::memset( counter, 0, 26 * sizeof( int ) );
int counter[26] = {};
for (int j = 0; j < sizeof(alphabet) - 1; ++j){
if (alphabet[j] == paragraph[i]){
counter[j]++;
}
for (int j = 0; j < sizeof(alphabet); ++j){
if (alphabet[j] == paragraph[i]){
counter[j]++;
}
for (int k = 0; k < sizeof(counter) - 1; ++k){
if (counter[k] > result){ result = counter[k]; }
}
const int N = 26;
char alphabet[N] =
{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
//...
for ( int j = 0; j < N; ++j )
{
if (alphabet[j] == paragraph[i]){
counter[j]++;
}
int ewsult = *std::max_element( counter, counter + 26 );