C++之运算符

  1 #include <iostream>
  2 #include<stdlib.h>
  3 #include<string>
  4 #include<vector>
  5 #include<cstring>
  6 #include<bitset>
  7 
  8 
  9 using  namespace std;
 10 
 11 //操作符
 12 
 13 int main()
 14 {
 15     //算数操作符
 16     int i=100;
 17     int j=200;
 18     cout<<j+i<<endl;
 19     cout<<j-i<<endl;
 20     cout<<j*i<<endl;
 21     cout<<j/i<<endl;
 22     cout<<j%i<<endl;
 23     int k=-i;
 24     cout<<k<<endl;
 25     //小心溢出
 26     short sv =32767;
 27     short ival=5;
 28     short l=sv+ival;
 29     cout<<l<<endl;
 30 //==========================================
 31     cout<<"关系和逻辑操作符"<<endl;
 32     cout<<"< <= > >= == !="<<endl;
 33     cout<<"&& || !"<<endl;
 34     int a,b;
 35     cout<<"请输入两个整数"<<endl;
 36     cin>>a>>b;
 37     if(a>b) cout<<a<<"大于"<<b<<endl;
 38     if(a<b) cout<<a<<"小于"<<b<<endl;
 39     if(a==b) cout<<a<<"等于"<<b<<endl;
 40     if(a!=b) cout<<a<<"不等于"<<b<<endl;
 41 
 42     bool x=true;
 43     bool y=false;
 44     if(x&&y)cout<<12<<endl;
 45     if(x||y)cout<<34<<endl;
 46     if(!x)cout<<56<<endl;
 47 
 48     cout<<"=========================="<<endl;
 49     int val;
 50     vector<int> vec;
 51     while(cin>>val && val!=42)
 52     {
 53         //cin>>val;
 54         vec.push_back(val);
 55     }
 56     for(vector<int>::iterator iter=vec.begin();iter!=vec.end();iter++)
 57     {
 58         cout<<*iter<<endl;
 59     }
 60     cout<<"=========================="<<endl;
 61     char *cp="Hello world";
 62     while(cp&&*cp)
 63     {
 64         cout<<*cp<<endl;
 65         ++cp;
 66     }
 67 
 68     cout<<"=========================="<<endl;
 69     cout<<"位操作符"<<endl;
 70 
 71     unsigned char bits=0227;
 72     bits=~bits; //取反
 73     cout<<(int)bits<<endl;
 74 
 75     bits=1; //00000001
 76     unsigned char result;
 77     result=bits<<1; //00000010
 78     cout<<(int)result<<endl;
 79     result=bits<<2; //00000100
 80     cout<<(int)result<<endl;
 81     bits=64; //01000000
 82     result=bits>>3;//00001000
 83     cout<<(int)result<<endl;
 84     //&位与 全为1则为1否则为0
 85     //|位或 全为0则为0否则为1
 86     // ^异或 相同为0不同为1
 87     //30个学生合格与否用二进制表示
 88     bitset<30> a;
 89     a.set(27);//第27位设置为1
 90     a.reset(22); //第22位变为0
 91 
 92     cout<<"=========================="<<endl;
 93     cout<<"赋值操作符"<<endl;
 94     //左操作数必须是非const值
 95     //可以连续赋值 右结合性
 96     string s1=s2="OK";
 97     cout<<"=========================="<<endl;
 98     cout<<"自增和自减操作符"<<endl;
 99     //++i i++ --i i--
100     int a,b;
101     a=2;
102     b=++a; //3
103     b=a++; //2
104     cout<<b<<endl;
105     cout<<"=========================="<<endl;
106     cout<<"箭头操作符"<<endl;
107     //(*p).foo() p->foo()
108      vector<string*> v;
109      string str1;
110      cout<<"enter input:"<<endl;
111      while(cin>>str1)
112      {
113          string *p=new string;
114          *p=str1;
115          v.push_back(p);
116      }
117      cout<<"output"<<endl;
118      vector<string*>::iterator it=v.begin();
119      while(it!=v.end() )
120      {
121          cout<<**it++<<"=="<<(*it)->size()<<endl;
122      }
123      it=v.begin();
124      while(it!=v.end())
125      {
126          delete[] *it;
127          it++;
128      }
129 
130     return 0;
131 }

运算符优先级:https://baike.baidu.com/item/%E8%BF%90%E7%AE%97%E7%AC%A6%E4%BC%98%E5%85%88%E7%BA%A7/4752611?fr=aladdin

原文链接: https://www.cnblogs.com/yh2924/p/12551961.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++之运算符

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/337090

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月1日 下午10:59
下一篇 2023年3月1日 下午10:59

相关推荐