Home  >  Article  >  Backend Development  >  How to output string type in c++

How to output string type in c++

下次还敢
下次还敢Original
2024-05-01 16:30:191019browse

The following methods are used to output the string type in C: use std::cout object: std::cout << string_variable uses the output operator (<<): string_variable << std: :cout

How to output string type in c++

Output string type in C

In C, you can use the following method to output string type :

  • std::cout Object:

    • std::cout << string_variable
  • Output operator (<<):

    • string_variable << std:: cout

##Example:

<code class="cpp">#include <iostream>
#include <string>

using namespace std;

int main() {
  string my_string = "Hello, C++!";

  // 使用 std::cout 对象输出
  cout << my_string << endl;

  // 使用输出运算符输出
  my_string << cout << endl;

  return 0;
}</p>Output:<p></p>
<pre class="brush:php;toolbar:false"><code>Hello, C++!
Hello, C++!</code>

Note:

    The two methods are essentially equivalent, but it is more common to use
  • std::cout object output. The
  • endl manipulator can be used for line breaks.

The above is the detailed content of How to output string type in c++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use compare in c++Next article:How to use compare in c++