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种,根据语句,从右往左进行,压栈,最后输出,出栈


目录
相关文章
|
Linux C++ 缓存
C/C++中printf/cout 计算顺序与缓冲区问题
1.printf/cout在同一个语句中都是从右向左计算的。 看如下的代码: 1 #include 2 int main() 3 { 4 int i=0; 5 printf("%d %d",i++,i++); 6 printf(" %d",i++); 7 return 0; 8 } 输出结果: 1 0 2 (第5行,先计算右边的i++,再计算左边的i++。
1366 0
C++(cout和printf的使用小结)
C++(cout和printf的使用小结)
|
算法 C++ iOS开发
|
C语言 C++
C++ 中的 cout.setf() 函数
C++ 中的 cout.setf() 函数
C++ 中的 cout.setf() 函数
cout输出小问题
#include using namespace std; int main(void) { float a = 12.234; int b = 12; cout.precision(1); cout
796 0
|
Unix C语言 iOS开发
C++cin,cout以及常见函数总结,cin,cout格式化控制
C++cin,cout以及常见函数总结,cin,cout格式化控制
C++cin,cout以及常见函数总结,cin,cout格式化控制
|
7月前
|
存储 监控 C++
cout.tellp()和cout.seekp()语法介绍
C++ 中的 `cout.tellp()` 用于获取输出流缓冲区的当前位置,而 `seekp()` 可以改变这个位置。数据先存入缓冲区,待缓冲区刷新后才输出。`tellp()` 返回一个表示位置的 `streampos`(通常可转换为整数),在无数据时为0,失败时为-1。`seekp()` 用于设置下一个字符的输出位置,接受一个位置参数或偏移量和方向,允许在缓冲区中移动并覆盖已有数据。例如,可以使用 `seekp()` 改变输出流中的部分数据,然后继续写入。
88 12
|
存储 监控 C++
C++ 的cout.tellp()和cout.seekp()语法介绍
无论是使用 cout 输出普通数据,用 cout.put() 输出指定字符,还是用 cout.write() 输出指定字符串,数据都会先放到输出流缓冲区,待缓冲区刷新,数据才会输出到指定位置(屏幕或者文件中)。 值得一提的是,当数据暂存于输出流缓冲区中时,我们仍可以对其进行修改。ostream 类中提供有 tellp() 和 seekp() 成员方法,借助它们就可以修改位于输出流缓冲区中的数据。 C++ tellp()成员方法 首先,tellp() 成员方法用于获取当前输出流缓冲区中最后一个字符所在的位置,其语法格式如下: streampos tellp(); 显然,tellp()
148 0
|
人工智能
1062. Talent and Virtue (25) 大量输入输出 scanf printf会比cin cout 省很多时间
#include #include #include using namespace std; struct node{ int id, v, t, g; }; vector v[4]; int cmp(node &a, node &b) { if (a.
1126 0
|
9月前
std::cout输出十六进制数据
std::cout输出十六进制数据
213 0

热门文章

最新文章