C++ 基本输入输出以及字符串的介绍
2018-03-30 20:10
836 查看
C++的基本输入和输出
1.最常规的输入和输出
cin >>
cout <<
这俩的头文件是
endl来表示换行,但这还有其他作用:
The endl manipulator produces a newline character, exactly as the insertion of ‘\n’ does; but it also has an additional behavior: the stream’s buffer (if any) is flushed, which means that the output is requested to be physically written to the device, if it wasn’t already. This affects mainly fully buffered streams, and cout is (generally) not a fully buffered stream. Still, it is generally a good idea to use endl only when flushing the stream would be a feature and ‘\n’ when it would not. Bear in mind that a flushing operation incurs a certain overhead, and on some devices it may produce a delay.
它会刷新输出缓冲区,这就意味着这输出需要被物理写入到设备。但这种缓冲区只针对完全缓冲流,而cout则不是完全缓冲流。用endl来代替
\n是个好习惯。
但执行刷新操作是有代价的,对设备带来一些延迟。
2.对字符串的输入输出
对字符串,可以使用字符数组,但这是很麻烦的。C++在中提供了字符串类型,可以直接定义。
使用
getline(cin,string),这种来输入进string的变量,这种输入方式是作为一行来输入的,可以有相关空格。
如果使用
cin >> a,这样也可以的,但以空格作为结束,意味着无法输入空格。
字符串可以进行相关拼接操作,直接用
+就能很好的解决,操作代码如下:
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string a,b; cin >> a; cin >> b; a=a+' '+b; cout << a << endl; return 0; }
3.字符串流
在中有stringstram()函数,可以将字符串类型转换为其他类型。string mystr ("1204"); int myint; stringstream(mystr) >> myint;
可以来进行有关数值转换。
相关文章推荐
- C++基本数据类型与输入输出
- C++字符数组的输入输出和字符串结束标志使用讲解
- 字符串和基本输入输出
- 习题 7.9 编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格、和其他字符的个数,在主函数中输入字符串以及输出上述的结果。
- C++输入一个字符串,把其中的字符按照逆序输出的两种方法
- java输入字符串输出首字母大写以及全拼
- (c++)输入三个字符串,按由小到大的顺序输出
- C++输入一个字符串,将其逆序输出
- C/C++的cmd下的编译运行以及文件输入输出
- 编写一个函数,由实参传来一个字符串,统计此字符串中字母,数字,空格,和其他字符的个数,在主函数中输入字符串以及输出上述统计的结果。再考虑将算的的结果放在一个数组中
- C++中的几种字符串输入输出方法
- 利用C++将输入一个字符串把其中的字符串逆向输出
- C/C++:递归使用:使用递归反向写文件(例子)以及字符串翻转输出
- c++ 输入字符串,输出数字
- 2.写出一个程序,接受一个有字母和数字以及空格组成的字符串,和一个字符,然后输出输入字符串中含有该字符的个数。不区分大小写。
- 二维字符串数组的输入、输出方式 以及冒泡法排序
- c++ 递归将输入的整数 转换成字符串输出
- c++ 输入一个字符串,将其逆序后输出。
- 输入字符串以及输出
- C++ 字符数组、字符串【输入、输出】