C++ 字符串变量声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4743564/
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
C++ String Variable Declaration
提问by Mike
I'm having some trouble declaring a string variable. Code and the errors are here: http://pastebin.com/TEQCxpZdAny thoughts on what I'm doing wrong? Also, please keep it platform independent. Thanks!
我在声明字符串变量时遇到了一些麻烦。代码和错误在这里:http://pastebin.com/TEQCxpZd 关于我做错了什么的任何想法?另外,请保持平台独立。谢谢!
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
string input; //Declare variable holding a string
input = scanf; //Get input and assign it to variable
printf(input); //Print text
return 0;
}
Getting this from GCC:
main.cpp: In function ‘int main()':
main.cpp:53:10: error: invalid conversion from ‘int (*)(const char*, ...)' to ‘char'
main.cpp:53:10: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]'
main.cpp:54:14: error: cannot convert ‘std::string' to ‘const char*' for argument ‘1' to ‘int printf(const char*, ...)'
回答by Naveen
You are mixing c++ and c I/O. In C++ this is,
您正在混合 c++ 和 c I/O。在 C++ 中,这是,
#include <string>
#include <iostream>
int main(void)
{
std::string input;
std::cin >> input;
std::cout << input;
return 0;
}
回答by dan04
cannot convert ‘std::string' to ‘const char*' for argument ‘1' to ‘int printf(const char*, ...)'
无法将参数 '1' 的 'std::string' 转换为 'const char*' 到 'int printf(const char*, ...)'
input = scanf; //Get input and assign it to variable
You're trying to assign the function pointerto scanf
to a string variable. You can't do that, which is why you're getting the first error. The proper syntax would be.
你试图将分配函数指针到scanf
一个字符串变量。你不能那样做,这就是你得到第一个错误的原因。正确的语法是。
char buffer[BIG_ENOUGH_SIZE];
scanf("%*s", sizeof(buffer) - 1, buffer);
input = buffer;
But that's a very C-style way of doing things. The idiomatic way to read input in C++ is with std::cin >> input
as Nathan suggested.
但这是一种非常 C 风格的做事方式。在 C++ 中读取输入的惯用方法是与std::cin >> input
Nathan 建议的一样。
cannot convert ‘std::string' to ‘const char*' for argument ‘1' to ‘int printf(const char*, ...)'
无法将参数 '1' 的 'std::string' 转换为 'const char*' 到 'int printf(const char*, ...)'
printf(input); //Print text
printf
takes a const char*
as its first argument, not a std::string
. You can use .c_str()
to convert to a C-style string. But neverpass user input as the first argument to printf
; the user can do nasty stuff by putting %
's in the string. If you insist on C-style output, the correct syntax is:
printf
将 aconst char*
作为它的第一个参数,而不是 a std::string
。您可以使用.c_str()
转换为 C 风格的字符串。但永远不要将用户输入作为第一个参数传递给printf
; 用户可以通过将%
's 放在字符串中来做讨厌的事情。如果你坚持 C 风格的输出,正确的语法是:
printf("%s", input.c_str());
But the C++-style alternative is std::cout << input;
.
但是 C++ 风格的替代方案是std::cout << input;
.
回答by dan04
I understand the question to be: How do you make a string declaration in C++? Here's a short program to demonstrate:
我理解的问题是:如何在 C++ 中进行字符串声明?这是一个简短的程序来演示:
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string your_name;
cout << "Enter your name: ";
cin >> your_name;
cout << "Hi, " << your_name << "!\n";
return 0;
}
So, include cstdlib at the start of your program. In practical terms, this means typing string instead of std::string, cout instead of std::cout and so on. The string variable itself (in the example, the string variable is your_name) is declared with string.
因此,在程序开始时包含 cstdlib。实际上,这意味着键入 string 而不是 std::string,cout 而不是 std::cout 等等。字符串变量本身(在示例中,字符串变量是 your_name)是用字符串声明的。
Let's say you've saved the program with the filename, 'str_example.cpp' To compile the program at the command line (in Linux):
假设您已使用文件名“str_example.cpp”保存程序,以在命令行(在 Linux 中)编译该程序:
g++ -o str_example str_example.cpp
This creates an executable object file called str_example (no file extension). And finally, assuming you're in the same directory as the program, to run it:
这将创建一个名为 str_example(无文件扩展名)的可执行对象文件。最后,假设你和程序在同一个目录下,运行它:
./str_example
The man page for g++ is extensive but not included by default. To install g++ documentation using the aptitude package manager:
g++ 的手册页很广泛,但默认情况下不包含在内。使用 aptitude 包管理器安装 g++ 文档:
sudo apt-get install gcc-7-doc
Note that the '7' refers to version 7; the current version at the time of writing. Hope that helps.
请注意,“7”指的是版本 7;撰写本文时的当前版本。希望有帮助。