龙空技术网

C++|输入输出的插入、提取操作符与重定向、移位操作

小智雅汇 150

前言:

眼前兄弟们对“c语言 移位”大概比较重视,大家都需要了解一些“c语言 移位”的相关资讯。那么小编在网上网罗了一些关于“c语言 移位””的相关知识,希望同学们能喜欢,看官们一起来了解一下吧!

为什么C++的输入输出使用">>"和"<<"这两个符号?操作系统的重定向操作符就是使用">",">>",如以下的windows平台的批处理(bat)文件:

chcp 65001echo ^<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >more.htmlecho  ";^> >>more.htmlecho ^<html xmlns=";^> >>more.htmlecho ^<base target="_blank" /^> >>more.htmlecho ^<meta content="text/html; charset=utf-8" /^> >>more.htmlecho ^<head^> >>more.htmlecho ^<title^>contents^</title^> >>more.htmlecho ^<link  href="../../more.css" rel="stylesheet" type="text/css" /^> >>more.htmlecho ^<style type=text/css^> >>more.htmlecho ^</style^>^</head^> >>more.htmlecho ^<body^>^<div^> >>more.htmlfor /f "tokens=1,2 usebackq delims=." %%a in (`dir  /o:n /b`) do (	if "%%a.%%b"=="%%a." (		echo ^<li^>^<a href="%%a/a.html"^>^<span style="color:blue; "^>%%a^</span^>^</a^>^</li^> >>more.html	))for /f "tokens=1,2 usebackq delims=." %%a in (`dir  /o:n /b`) do (	if not "%%a.%%b"=="%%a." (	if not "%%a.%%b"=="more.html" (	if not "%%b"=="bat" (	    if "%%b"=="html" (		echo ^<li^>^<a href="%%a.%%b"^>%%a^</a^>^</li^> >>more.html            )	)	)	))for /f "tokens=1,2 usebackq delims=." %%a in (`dir  /o:n /b`) do (	if not "%%a.%%b"=="%%a." (	if not "%%a.%%b"=="more.html" (	if not "%%b"=="bat" (	    if not "%%b"=="html" (		echo ^<li^>^<a href="%%a.%%b"^>%%a.^<span style="color:red; "^>%%b^</span^>^</a^>^</li^> >>more.html            )	)	)	))echo ^</div^> >>more.htmlecho ^</body^> >>more.htmlecho ^</html^> >>more.html

在C中,">>"和"<<"用于表示移位操作。

在C++中,有重载这一概念,C++便重载了">>"和"<<",注意重载不能改变运算符的优先级和结合性。移位操作是一种特殊的算术运算(某变量左移n位在一定情形下等于该变量乘以2的n次幂,右移相当于除法操作。

msb << 4 + lsb

优先级问题

表达式

人们可能误以为的结果

实际结果

算术运算高于移位运算符

msb << 4 + lsb

(msb << 4) + lsb

msb << (4 + lsb)

流插入和提取运算符<<、>>是对位移运算符的重载,重载不能改变其优先级。

std::cout << (3 & 5); //<<相对于&,具有较高的优先级

">>"和"<<"既能用于位运算的移位操作,也用于输入输出操作:

#include <iostream>#include <fstream>#include <stdlib.h>using namespace std;void fileOutput(char* fn){    ofstream ofs(fn);    ofs<<"<!doctype html>\n";    ofs<<"<html>\n";    ofs<<"<head>\n";    ofs<<"<title>the 1st webpage</title>\n";    ofs<<"<style>\n";    ofs<<"p{\n";    ofs<<"  width:60%;\n";    ofs<<"  margin:auto;}\n";    ofs<<"</style>\n";    ofs<<"</head>\n";    ofs<<"<body>\n";    ofs<<"  <p>hello world!</p>\n";    ofs<<"</body>\n";    ofs<<"</html>\n";}int main(){    fileOutput("index.html");    system("index.html");    return 0;}/*<!doctype html><html><head><title>the 1st webpage</title><style>p{  width:60%;  margin:auto;}</style></head><body>  <p>hello world!</p></body></html>*/

-End-

标签: #c语言 移位 #c语言移位操作符