cout,printf的++,--优先问题

简介: cout,printf的++,--优先问题
#include<iostream>
using namespace std;
int n;
class myint {
  friend ostream& operator<<(ostream& cout, myint mi);
private:
  int mynum;
  string name;
public:
  myint() {
    mynum = 0;
    name = "*********";
  }
  myint& operator++() {
    n++;
    mynum++;
    cout <<"第"<<n<<"次"<< "前置++" << endl;
    return *this;
  }
  myint& operator--() {
    n++;
    mynum--;
    cout << "第" << n << "次" << "前置--" << endl;
    return *this;
  }
  myint operator++(int) {
    n++;
    myint temp = *this;
    mynum++;
    cout << "第" << n << "次" << "后置++" << endl;
    return temp;
  }
  myint operator--(int) {
    n++;
    myint temp = *this;
    mynum--;
    cout << "第" << n << "次" << "后置--" << endl;
    return temp;
  }
};
ostream& operator<<(ostream& cout, myint me) {
  n++;
  cout <<"a="<< me.mynum << me.name <<"n="<< n<<endl;
  return cout;
}
int main() {
  myint me;
  cout << --me << me++  << me-- << ++me << endl;
  int a = 0;
}

第1次前置++

第2次后置–

第3次后置++

第4次前置–

a=0*********n=5

a=0*********n=6

a=1*********n=7

a=1*********n=8

非常的有意思,直接说结论,++,- -前置和后置,4种,根据语句,从右往左进行,压栈,最后输出,出栈


目录
相关文章
VS中出现的printf,scanf等函数不安全而报错的问题的全面解决方法
VS中出现的printf,scanf等函数不安全而报错的问题的全面解决方法
|
1月前
|
存储 监控 C++
cout.tellp()和cout.seekp()语法介绍
C++ 中的 `cout.tellp()` 用于获取输出流缓冲区的当前位置,而 `seekp()` 可以改变这个位置。数据先存入缓冲区,待缓冲区刷新后才输出。`tellp()` 返回一个表示位置的 `streampos`(通常可转换为整数),在无数据时为0,失败时为-1。`seekp()` 用于设置下一个字符的输出位置,接受一个位置参数或偏移量和方向,允许在缓冲区中移动并覆盖已有数据。例如,可以使用 `seekp()` 改变输出流中的部分数据,然后继续写入。
44 12
|
1月前
|
算法 iOS开发 C++
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 的作用和注意事项
默认情况下,C++的输入输出流与C标准库的输入输出函数是同步的,这会造成一定的性能损失。:使用cin.tie(0)和cout.tie(0)可以取消cin与cout之间的绑定,这意味着在进行输入操作时,不需要强行刷新输出缓冲区。:如果你的程序在输入输出中同时使用了C++的输入输出流和C标准库的输入输出函数(如scanf和printf),则不应该使用这段代码。:在使用了这段代码后,应避免使用C标准库的输入输出函数(如printf和scanf),因为这些函数与输入输出流的同步已被关闭。这段代码的主要用途是。
23 1
|
3月前
|
C语言
使用printf函数输出数据
在C语言中,printf函数是一个常用的标准库函数,用于在控制台输出格式化的字符串和数据。它允许我们按照指定的格式输出各种类型的数据,包括整数、浮点数、字符和字符串等。
29 0
|
8月前
|
安全 C语言 C++
VS中使用scanf函数报错如何解决?
VS中使用scanf函数报错如何解决?
35 0
|
9月前
|
C语言
printf与scanf函数的返回值
printf与scanf函数的返回值
|
11月前
|
存储 监控 C++
C++ 的cout.tellp()和cout.seekp()语法介绍
无论是使用 cout 输出普通数据,用 cout.put() 输出指定字符,还是用 cout.write() 输出指定字符串,数据都会先放到输出流缓冲区,待缓冲区刷新,数据才会输出到指定位置(屏幕或者文件中)。 值得一提的是,当数据暂存于输出流缓冲区中时,我们仍可以对其进行修改。ostream 类中提供有 tellp() 和 seekp() 成员方法,借助它们就可以修改位于输出流缓冲区中的数据。 C++ tellp()成员方法 首先,tellp() 成员方法用于获取当前输出流缓冲区中最后一个字符所在的位置,其语法格式如下: streampos tellp(); 显然,tellp()
99 0
C++(cout和printf的使用小结)
C++(cout和printf的使用小结)
|
C++
C++ 重载操作符<<实现cout定位输出以及设置颜色:cout<<Goto(x,y)<<setC(color)<<123<<cr;
C++ 重载操作符<<实现cout定位输出以及设置颜色:cout<<Goto(x,y)<<setC(color)<<123<<cr;
216 0
|
C语言 C++
C++ 中的 cout.setf() 函数
C++ 中的 cout.setf() 函数
C++ 中的 cout.setf() 函数