Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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
使用for循环打印字符串数组不';t功能正常(C+;+;) 我是一个C++新手,只是想玩几个东西,在这个例子中是专门的数组。我想在控制台上打印一个字符串数组,但它要么以二进制格式打印随机内容,要么告诉我有分段错误,要么什么也不做_C++_Arrays_For Loop_Sizeof - Fatal编程技术网

使用for循环打印字符串数组不';t功能正常(C+;+;) 我是一个C++新手,只是想玩几个东西,在这个例子中是专门的数组。我想在控制台上打印一个字符串数组,但它要么以二进制格式打印随机内容,要么告诉我有分段错误,要么什么也不做

使用for循环打印字符串数组不';t功能正常(C+;+;) 我是一个C++新手,只是想玩几个东西,在这个例子中是专门的数组。我想在控制台上打印一个字符串数组,但它要么以二进制格式打印随机内容,要么告诉我有分段错误,要么什么也不做,c++,arrays,for-loop,sizeof,C++,Arrays,For Loop,Sizeof,最初的程序有点复杂,但我试图尽量减少额外的东西,以便我可以看到问题是什么,但没有成功 #包括 #包括 使用名称空间std; int main(){ 字符串stArr[]={“1 |”、“|”、“|”、“|”、“|”、“|”、“|”}; 对于(int i=0;i循环是充满危险的。谢谢,它工作了!我不完全理解:Sigeof(String)考虑每个字符串的大小,或者它具体是如何工作的?因为我不必指定要考虑的字符串。@splashrt检查sizeof(stArr[0]),sizeof(stArr[1])

最初的程序有点复杂,但我试图尽量减少额外的东西,以便我可以看到问题是什么,但没有成功

#包括
#包括
使用名称空间std;
int main(){
字符串stArr[]={“1 |”、“|”、“|”、“|”、“|”、“|”、“|”};
对于(int i=0;icout检查,sizeof(strArray
)的值是多少。我打赌您假定它是7(即数组中的字符串数)

扰流器:很明显不是这样。
sizeof(strArray)
返回整个数组占用的内存量,即7个字符串。它是一个大于7的数字,因此在迭代过程中读取数组边界之外的内存


将其除以一个元素的大小(例如
sizeof(strArray)/sizeof(string)
),得到要导出的大小。

for循环中的此条件

i < sizeof(stArr)
因此,要获得数组中元素的数量,知道数组的大小和元素的大小,您可以编写

N = sizeof( array ) / sizeof( T )
但使用基于范围的for循环要简单得多

for ( const auto &s : stArr ) std::cout << s;
否则,您可以按照以下方式使用头
中定义的结构
std::extent

#include <iostream>
#include <string>
#include <type_traits>

using namespace std;

int main() {

    string stArr[] = {" 1 | ","_"," | ","_"," | ","_"," | "};
    for ( size_t i = 0; i < extent<decltype( stArr )>::value; i++) {
           cout << stArr[i];
    }
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串stArr[]={“1 |”、“|”、“|”、“|”、“|”、“|”、“|”};
对于(大小i=0;isizeof(stArr)
提供整个数组的总字节大小。它不提供元素计数,而元素计数正是您要查找的。因此,您的循环超出了数组的边界,这就是您在输出中得到垃圾的原因

要获取数组的元素计数,需要将数组的字节大小除以数组中单个元素的大小,例如:

#包括
#包括
int main(){
std::string stArr[]={“1 |”、“|”、“|”、“|”、“|”、“|”、“|”};
int count=sizeof(stArr)/sizeof(stArr[0]);
对于(int i=0;iSTD:斯塔尔,你知道什么是<代码> <代码> >返回吗?如果是,请检查它。这就是为什么C++时你应该使用<代码> STD::向量或<代码> STD::数组< /代码>和适当枚举器。C风格<代码> < /COD>循环是充满危险的。谢谢,它工作了!我不完全理解:Sigeof(String)考虑每个字符串的大小,或者它具体是如何工作的?因为我不必指定要考虑的字符串。@splashrt检查
sizeof(stArr[0])
sizeof(stArr[1])
等的结果(即-sizeof
为数组中的连续字符串返回的结果)。这些真的不同吗?将其与
stArr[0].length()
stArr[1].length()
等的结果进行比较。通常,您可以将
sizeof(string)
视为存储字符串信息的对象的大小(字节),而不是字符串本身。
for ( const auto &s : stArr ) std::cout << s;
#include <iostream>
#include <string>
#include <iterator>

using namespace std;

int main() {

    string stArr[] = {" 1 | ","_"," | ","_"," | ","_"," | "};
    for ( size_t i = 0; i < size( stArr ); i++) {
           cout << stArr[i];
    }
}
#include <iostream>
#include <string>
#include <type_traits>

using namespace std;

int main() {

    string stArr[] = {" 1 | ","_"," | ","_"," | ","_"," | "};
    for ( size_t i = 0; i < extent<decltype( stArr )>::value; i++) {
           cout << stArr[i];
    }
}
#include <iostream>
#include <string>
#include <vector>

int main() {

    std::vector<std::string> stArr;
    stArr.push_back(" 1 | ");
    stArr.push_back("_");
    stArr.push_back(" | ");
    stArr.push_back("_");
    stArr.push_back(" | ");
    stArr.push_back("_");
    stArr.push_back(" | ");

    std::size_t count = stArr.size();

    for(size_t i = 0; i < count; ++i) {
        std::cout << stArr[i];
    }

    return 0;
}