c++基础复习之表达式

  1. 算数操作符
操作符功能用法
-、+一元正号、负号+expr、-expr
/、*、 %乘法、除法、求余a*a、a/a、a%a
+、-加法、减法a-a、a+a

优先级:一元操作优先级最高,其次是乘除余,最后是加减。

  1. 位操作符
    image
    • 位操作符可以将整型操作符视为二进制位的集合,而且还可以作用于bitset类型。
    • 如果位操作符的操作数为负数,则位操作符如何处理其操作数的符号要依赖于机器。因此,强烈建议使用unsigned整型操作数。
bits = ~bits; //位求反
bits << 1;    //左移一位
bits >> 2;    //右移2位
  1. 自增自减操作符
     前自增(++ a)操作和后自增(a ++)操作都使其操作数加1,二者的差别在于:前自增操作将修改后操作数的值作为表达式的结果值;而后自增操作将操作数原来的、未修改的值作为表达式的结果值。

  2. 箭头操作符
     箭头操作符包含点操作和解引用操作符的表达式。

(*p).foo;//解引用的优先级低于点操作符
p->foo;  //与上一种操作一样

习题:

/*定义一个vector对象,其每个元素指向string指针,读取vector对象,
输出每个string的内容以及相应的长度*/
#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
    //定义vector对象,每个元素指向string指针
    vector<string*> spvec;
    string str;
    cout << "Enter some strings (Ctrl+Z to end)" << endl;
    while(cin >> str)
    {
        string *pstr = new string;//指向string对象的指针
        *pstr = str;
        spvec.push_back(pstr);
    }
    //读取vector对象,输出内容及长度
    vector<string*>::iterator iter = spvec.begin();
    while(iter != spvec.end())
    {
        cout << **iter << " 长度为 " << (**iter).size() <<endl;
        iter++;
    }
    //释放每个内存空间
    iter = spvec.begin();
    while(iter != spvec.end())
        delete *iter++;

    return 0;
}
  1. 条件操作符
     条件表达式的优先级相当低,通常都需要用圆括号将表达式扩起来。
cout << (i < j ? i : j) << endl;  
cout << (i < j) ? i : j << endl;     //ERROR  
cout << i < j ? i : j << endl;       //ERROR  

习题:

/*处理vector<int>对象的元素,将每个奇数值元素用该值的两倍替换*/
#include<iostream>
#include<vector>

using namespace std;

int main()  
{  
    vector<int> ivec;  
    int value;  
    while (cin >> value)  
    {  
        ivec.push_back(value);  
    }  

    for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)  
    {  
        *iter = (*iter % 2 ? *iter * 2 : *iter);  
        cout << *iter << ' ';  
    }  
    cout << endl;  
}  
  1. sizeof操作符
    sizeof操作符返回一个对象或类型名的长度,返回值类型为size_t,长度单位为字节。
    sizeof表达式的结果是编译时常量。
//输出每种内置类型的长度
#include <iostream>
using namespace std;
int main()
{
cout << "type\t\t\t" << "size" << endl
<< "bool\t\t\t" << sizeof(bool) << endl
<< "char\t\t\t" << sizeof(char) << endl
<< "signed char\t\t" << sizeof(signed char) << endl
<< "unsigned char\t\t" << sizeof(unsigned char) << endl
<< "wchar_t\t\t\t" << sizeof(wchar_t) << endl
<< "short\t\t\t" << sizeof(short) << endl
<< "signed short\t\t" << sizeof(signed short) << endl
<< "unsigned short\t\t" << sizeof(unsigned short) << endl
<< "int\t\t\t" << sizeof(int) << endl
<< "signed int\t\t" << sizeof(signed int) << endl
<< "unsigend int\t\t" << sizeof(unsigned int) << endl
<< "long\t\t\t" << sizeof(long) << endl
<< "sigend long\t\t" << sizeof(signed long) << endl
<< "unsigned long\t\t" << sizeof(unsigned long) << endl
<< "float\t\t\t" << sizeof(float) << endl
<< "double\t\t\t" << sizeof(double) << endl
<< "long double\t\t" << sizeof(long double) << endl;
return 0;
}
  • 逗号操作符
    逗号表达式是一组由逗号分割的表达式,这些表达式从左到右进行计算。然而,逗号表达式的结果是其最右边表达式的值。

  • 复合表达式中优先级和结合性决定了表达式的哪个部分用作哪个操作符的操作数,但是还可以通过使用圆括号强制实现某个特殊的分组,因为圆括号凌驾于优先级之上。

  • 求值顺序
    除了&&、||与?:运算符外,未指定操作符的运算顺序。当操作符的两个操作数涉及到同一个对象,并改变其值时,操作数的计算顺序才会影响结果。C++并不保证计算是从左到右的。
    在一个表达式里,不要在两个或更多的子表达式中对同一个对象做自增或自减操作。

  • new和delete表达式

  • 动态创建对象的初始化

int i(1024); //value of i is 1024
int *pi = new int(1024); //object to which pi points is 1024
string s(5,'0'); //value of s is "00000"
string *ps = new string(5,'0'); //*ps is"00000"
  • 撤销动态创建的对象
    动态创建的对象用完后,必须显示地将该对象占用的内存返回给自由存储区。
delete pi; //释放pi指向的int型对象所占用的内存空间

习题:

//下列语句哪些是非法的
vector<string> svec(10);  
vector<string> *pvec1 = new vector~~<string>(10);  
vector<string> **pvec2 = new vector<string>[10]; /*错误在于:pvec2是指向元素类型为string的vector对象的指针的指针(即pvec2的类型为vector<string>**),而new 操作返回的是一个指向元素类型为string的 vector对象的指针,不能用于初始化pvec2。*/  
vector<string> *pv1 = &svec;  
vector<string> *pv2 = pvec1;  

delete svec;     
delete pvec1;  
delete []pvec2; /*错误在于:svec是一个vector对象,不是指针,不能对它进行delete操作。*/
delete pv1;      
delete pv2;  

动态内存常见管理错误:
- 删除(delete)指向动态分配内存的指针失败,因而无法将该内存返还给自由存储区,通常称为“内存泄漏(memory leak)”,这类错误很难发现,需
要直到所有内存耗尽,内存泄漏才会显露出来!
- 读写已经删除的对象。如果释放了指针所指向的对象之后,将指针置为0值,则比较容易检测出这类错误。
- 对同一个内存空间delete两次。当两个指针执行同一个动态创建的对象,删除时就发生错误,此时自由存储区可能会被破坏。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wcyd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值