Home > Backend Development > C++ > body text

How to output string type in c++

下次还敢
Release: 2024-05-01 16:30:19
Original
980 people have browsed it

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!