Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在for循环之后,字符串数据似乎被删除_C++_Arrays_String_Data Loss - Fatal编程技术网

C++ 在for循环之后,字符串数据似乎被删除

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

在下面的代码中,每当我转到调试器时,段落的值都会被删除,或者返回到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' };
    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;clearcout总之,所有问题都是由于您误用了
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 );