C++ 使用 cout 打印字符数组的全部内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19371845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using cout to print the entire contents of a character array
提问by Chandy
I am quite new to C++ (just a shaky background in Java) and I'm stumped about how to print out the entire contents of a char array. I believe I need to use a loop, and base the loop on the length of the array, but my attempts to compile aren't meeting with success. This is what I have right now. Thanks in advance for your help!
我对 C++ 很陌生(只是在 Java 中的背景摇摇欲坠),我对如何打印出 char 数组的全部内容感到困惑。我相信我需要使用一个循环,并将循环建立在数组的长度上,但是我的编译尝试没有成功。这就是我现在所拥有的。在此先感谢您的帮助!
#include <iostream>
#include <string>
using namespace std;
void namePrinting(char name[])
{
int i = 0;
cout << "Name: ";
while(i <= name.length() )
{
cout << name[i];
i++;
}
}
int main()
{
string fullName;
cout << "Enter name: ";
cin >> fullName;
char nameArray[fullName.length()];
namePrinting(nameArray);
}
回答by Beta
Start with something simple:
从简单的事情开始:
char c_array[3];
c_array[0] = 'a';
c_array[1] = 'b';
c_array[2] = 'c';
for(int i=0 ; i<3 ; ++i)
{
cout << c_array[i];
}
cout << endl;
Do not go farther until you understand that much perfectly.Now notice that if you null-terminate the array, you can pass the whole thing to cout
, and operator<<
will know when to stop:
在你完全理解之前不要走得更远。现在请注意,如果您以空值终止数组,则可以将整个内容传递给cout
,并且operator<<
知道何时停止:
char c_array[4];
c_array[0] = 'a';
c_array[1] = 'b';
c_array[2] = 'c';
c_array[3] = 0;
cout << c_array << endl;
You cannot do that with arrays of most other types.Now notice that you can assign a char[]
this way, and it will be null-terminated:
对于大多数其他类型的数组,您无法做到这一点。现在请注意,您可以通过char[]
这种方式分配 a ,并且它将以空值结尾:
char c_array[20] = "abc";
cout << c_array << endl;
You can even omit the size of the array, and the compiler will infer it:
你甚至可以省略数组的大小,编译器会推断出来:
char c_array[] = "abc"; // this is a char[4];
cout << c_array << endl;
There are a couple of different ways to read user input into an array, but it sounds as if you know that already, and this answer is getting long.
有几种不同的方法可以将用户输入读入数组,但听起来好像您已经知道了,而且这个答案越来越长。
回答by ddevienne
Writing each character individually using operator<<(char)
is inefficient.
使用单独编写每个字符operator<<(char)
效率低下。
Converting to an std::string
using the (const char*, size_t)
constructor, and writing that using operator<<(const std::string&)
is also inefficient.
转换为std::string
using(const char*, size_t)
构造函数,并编写 usingoperator<<(const std::string&)
也是低效的。
The proper way is simply to use http://en.cppreference.com/w/cpp/io/basic_ostream/write
正确的方法是简单地使用http://en.cppreference.com/w/cpp/io/basic_ostream/write
PS: Note that your code is not valid C++. char name[]
is basically synonymous with char* name
and doesn't know its length (and there's no .length()
on it too). And your nameArray
is not initialized. Sized, yes; initialized, no. You're missing a std::copy
or strncpy
call to copy the content of fullName
into nameArray
.
PS:请注意,您的代码不是有效的 C++。char name[]
基本上是同义词char* name
并且不知道它的长度(并且也没有.length()
)。而你nameArray
的没有初始化。大小,是的;初始化,没有。你缺少一个std::copy
或strncpy
打电话的内容复制fullName
到nameArray
。
回答by Al2O3
printf("%s", nameArray);
just works!
只是工作!
回答by James Yen
For my problem, my program was looking for a command line parameter. Since I did not provide the parameter, so it throws terminate called after throwing an instance of std::logic_error what(): basic_string::_S_construct null not valid
对于我的问题,我的程序正在寻找命令行参数。由于我没有提供参数,所以它在抛出一个实例后抛出终止调用std::logic_error what(): basic_string::_S_construct null not valid
I hope this gives you some insight on how to figure your problem.
我希望这能让您对如何解决问题有所了解。
回答by ppetraki
Here's a generic way to do it.
这是一个通用的方法。
// write out any c style char array to an output stream
#include <iostream>
#include <string.h>
void write_char_array(std::ostream& os, const char* string) {
// faster than an ostream_iterator and std::copy
os.write(string, strlen(string));
}
int main()
{
const char question[] = "What is your name? ";
const char* answer = "Bob";
write_char_array(std::cout, question);
write_char_array(std::cout, answer);
}
Output:
输出:
What is your name? Bob