c++指针与集合类介绍使用

版权声明:旨在学习交流,共同进步 https://blog.csdn.net/u013735511/article/details/82584578

前沿

c++是一个重要的工具,涉及到性能问题的都会使用到c++,遗憾的是自己对c++相关的东西不太了解,希望借助这波学习能弄通它。

c++指针

首先弄懂两个符号的意思,&、*。

&

每一个变量都有一个内存位置,每一个内存位置都定义了可使用连字号(&)运算符访问的地址,它表示了在内存中的一个地址。请看下面的实例,它将输出定义的变量地址

#include <iostream>
using namespace std;
int main(){

    int var1;
    char var2[10];
    cout << "地址1:" << &var1 << endl;
    cout << "地址1:" << &var2 << endl;
}
*

指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址。就像其他变量或常量一样,您必须在使用指针存储其他变量地址之前,对其进行声明。

#include <iostream>
using namespace std;
int main(){

    int var1 = 40;
    int *p1;
    p1 = &var1;
    cout << p1 << endl;
    cout << *p1 << endl;
}

执行结果为

➜  code ./a.out
0x7ffee586f6ac
40

可以看到,p1是地址,*p1就是值了。我们再看看它复杂的使用方式。

#include <iostream>
using namespace std;
struct info{
    int a;
    int b;
};
int main(){

    int var1 = 40;
    info *p1 = new info();
    *p1 = {1,2};
    cout << p1->a << endl;
    cout << p1->b << endl;
    int *p2;
    p2 = &var1;
    cout << *p2 << endl;
    *p2 = 3;
    cout << *p2 << endl;
    delete p2;
    delete p1;
}

注意使用g++ -std=c++14 test.cpp进行编译

c++指针传值

#include <iostream>
using namespace std;
struct info{
    int a;
    int b;
};

void disp(info *p1) {
    cout << p1->a << endl;
    cout << p1->b << endl;
}

int main(){

    int var1 = 40;
    info *p1 = new info();
    *p1 = {1,2};
    disp(p1);
    delete p1;
}

以上代码可以正常执行,同时也可以这样

#include <iostream>
using namespace std;
struct info{
    int a;
    int b;
};

void disp(info p1) {
    cout << p1.a << endl;
    cout << p1.b << endl;
}

int main(){

    int var1 = 40;
    info *p1 = new info();
    *p1 = {1,2};
    disp(*p1);
    delete p1;
}

*p1就是一个value,而p1是一个指针。再看看下面的代码

#include <iostream>
using namespace std;

void disp(int *a){
    cout << *a << endl;
}

int main(){

    int var1 = 40;
    disp(&var1);
}

打印了40

#include <iostream>
using namespace std;

void disp(int* a){
    cout << a << endl;
}

int main(){

    int var1 = 40;
    disp(&var1);
}

打印了0x7ffeef2286cc

扫描二维码关注公众号,回复: 3242582 查看本文章

可以看到使用&,可以获取到变量的地址,也就是一个指针。

集合类的使用

下面以map为例子

#include <iostream>
#include <map>

using namespace std;

void disp(std::map<std::string, int> m) {
   for(auto it=m.begin(); it != m.end(); it++){
        cout<<"key: "<<it->first <<" value: "<<it->second<<endl;
   }
}

void disp(std::map<char, int> m) {
   for(auto it=m.begin(); it != m.end(); it++){
        cout<<"key: "<<it->first <<" value: "<<it->second<<endl;
   }
}

void del(std::map<std::string, int> m, std::string key) {
    std::map<std::string, int>::iterator it = m.find(key);
    if(it != m.end()){
        m.erase(key);
    }
    std::cout << m.size() << std::endl;
}

void del(std::map<std::string, int> *m, std::string key) {
    std::map<std::string, int>::iterator it = m->find(key);
    if(it != m->end()){
        m->erase(key);
    }
    std::cout << m->size() << std::endl;
}

int main(){

    std::map<char, int> first;
    first['a'] = 1;
    first['b'] = 2;
    std::cout << first.size() << std::endl;
    disp(first);
    std::map<std::string, int> second;
    second["ab"] = 4;
    second["bc"] = 5;
    second["ac"] = 5;
    std::cout << second.size() << std::endl;
    disp(second);
    del(second, "ac");
    std::cout << second.size() << std::endl;
    del(&second, "ac");
    std::cout << second.size() << std::endl;
}

返回结果为

2
key: a value: 1
key: b value: 2
3
key: ab value: 4
key: ac value: 5
key: bc value: 5
2
3
2
2

那c++ std里面有哪些集合类呢
这里写图片描述

集合类中还有很多方法,需要在实践中去运用~慢慢来吧

猜你喜欢

转载自blog.csdn.net/u013735511/article/details/82584578
今日推荐