程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++學習筆記(字符串string、vector_deque、queue,multiset、map、multimap、容器拷貝問題)(復制粘貼,方便後面翻閱)

C++學習筆記(字符串string、vector_deque、queue,multiset、map、multimap、容器拷貝問題)(復制粘貼,方便後面翻閱)

編輯:關於C++

1.string操作

#include 
#include 
#include  //算法

using namespace std;

//STL standard template libary 標准模(mu)板庫 C++一部分,編譯器自帶
//Android NDK支持
//java.lang java.util包中API,java的一部分

//string初始化

void main()
{
    //string由c字符串衍生過來的
    string s1 = "craig david";
    string s2("7 days");
    string s3 = s2;
    string s4(10,'a');

    cout << s4 << endl;

    system("pause");
}

//string遍歷
void main()
{
    string s1 = "craig david";
    //           ^
    //1 數組方式
    for (int i = 0; i < s1.length(); i++)
    {
        cout << s1[i] << endl;
    }
    //2 迭代器指針
    for (string::iterator it = s1.begin(); it != s1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    //3 at函數(charAt)
    // 可能會拋出異常
    try
    {
        for (int i = 0; i < s1.length() + 3; i++)
        {
            cout << s1.at(i) << " ";
        }
    }
    catch (...)
    {
        cout << "異常" << endl;
    }


    system("pause");
}


//string字符串->c字符串轉換
void main()
{
    //string -> char*
    string s1 = "walking away";
    const char* c = s1.c_str();
    printf("%s\n",c);

    //
    string s2 = c;

    //string->char[]
    //從string中賦值字符到char[]
    char arr[50] = {0};
    s1.copy(arr,4,0);

    cout << arr << endl;

    system("pause");
}


//字符串拼接
void main()
{
    string s1 = "alan";
    string s2 = "jackson";

    //1.
    string s3 = s1 + s2;

    string s4 = " pray";

    //2.
    s3.append(s4);

    cout << s3 << endl;

    system("pause");
}

//字符串查找替換
void main()
{
    string s1 = "apple google apple iphone";
    //從0開始查找"google"的位置
    int idx = s1.find("google", 0);
    cout << idx << endl;

    //統計apple出現的次數
    int idx_app = s1.find("apple",0);
    //npos大於任何有效下標的值
    int num = 0;
    while (idx_app != string::npos)
    {
        num++;
        cout << "找到的索引:" << idx_app << endl;
        idx_app+=5;
        idx_app = s1.find("apple", idx_app);
    }

    cout << num << endl;
    system("pause");
}

void main()
{
    string s1 = "apple google apple iphone";
    //0-5(不包含5)替換為jobs
    s1.replace(0, 5, "jobs");
    cout << s1 << endl;

    //所有apple替換為jobs
    int idx = s1.find("apple", 0);
    while (idx != string::npos)
    {
        s1.replace(idx, 5, "jobs");
        idx += 5;
        idx = s1.find("apple", idx);
    }

    cout << s1 << endl;
    system("pause");
}

//刪除(截取)和插入
void main()
{
    string s1 = "apple google apple iphone";
    //刪除a,找到a所在的指針
    string::iterator it = find(s1.begin(),s1.end(),'g');
    //只能刪除一個字符
    s1.erase(it);

    //開頭末尾插入字符串
    s1.insert(0, "macos");
    s1.insert(s1.length(), " facebook");

    cout << s1 << endl;
    system("pause");
}

//java StringBuffer才可變
//String 不可變
//大小寫轉換
void main()
{
    string s1 = "JASON";
    //原始字符串的起始地址,原始字符串的結束地址, 目標字符串的起始地址, 函數名稱
    transform(s1.begin(), s1.end()-1,s1.begin(), tolower);
    cout << s1 << endl;


    transform(s1.begin(), s1.end() - 1, s1.begin(), toupper);
    cout << s1 << endl;

    system("pause");
}

2.容器vector

//容器
//Vector
//初始化
#include 
void printVector(vector &v)
{
    //通過數組的方式遍歷
    for (int i = 0; i < v.size(); i++)
    {
        cout << v[i] << endl;
    }
}
void main()
{
    //1.
    vector v1;
    v1.push_back(20);
    v1.push_back(40);
    v1.push_back(15);
    v1.push_back(7);

    //2.
    vector v2 = v1;

    //3.部分復制
    vector v3(v1.begin(),v1.begin()+2);

    printVector(v3);


    system("pause");
}

//添加 刪除
void main()
{
    //添加到結尾
    vector v1;
    v1.push_back(20);
    v1.push_back(40);
    v1.push_back(15);
    v1.push_back(7);

    //訪問頭部
    v1.front() = 11;
    //訪問尾部
    v1.back() = 90;

    //刪除結尾的元素
    //v1.pop_back();
    while (v1.size() > 0)
    {
        cout << "末尾的元素:" << v1.back() << endl;
        v1.pop_back();
    }

    printVector(v1);

    system("pause");
}

//數組的方式
void main()
{
    vector v1;
    v1.push_back(20);
    v1.push_back(40);
    v1.push_back(15);
    v1.push_back(7);

    v1[2] = v1[2] +10;

    //容器等價於動態數組 
    vector v2(10);
    for (int i = 0; i < v2.size(); i++)
    {
        v2[i] = i + 1;
    }

    printVector(v2);

    system("pause");
}

//迭代器遍歷
//迭代器的種類(正向,反向迭代器)
void main()
{
    vector v1;
    v1.push_back(20);
    v1.push_back(40);
    v1.push_back(15);
    v1.push_back(7);
    //正向
    for (vector::iterator it = v1.begin(); it < v1.end(); it++)
    {
        cout << *it << endl;
    }
    cout << "-----------------" << endl;
    //反向迭代
    for (vector::reverse_iterator it = v1.rbegin(); it < v1.rend(); it++)
    {
        cout << *it << endl;
    }

    system("pause");
}

//刪除
void main()
{
    vector v1(10);
    for (int i = 0; i < v1.size(); i++)
    {
        v1[i] = i + 1;
    }

    //刪除指定位置
    vector::iterator it = v1.begin();
    it += 3;
    v1.erase(it);

    //distance(v1.begin(), it);

    //刪除區間
    v1.erase(v1.begin(), v1.begin() + 3);

    for (vector::iterator it = v1.begin(); it < v1.end(); it++)
    {
        if (*it == 5)
        {       
            printf("%x\n", it);
            vector::iterator tmp = v1.erase(it); //注意以後開發中編譯器版本問題
            printf("%x,%x\n",it,tmp);
        }
    }

    //插入
    v1.insert(v1.begin() + 2, 100);
    v1.insert(v1.end() - 1, 200);

    printVector(v1);

    system("pause");
}

3.隊列deque

//雙向隊列
#include 
void printDeque(deque& q)
{
    for (int i = 0; i < q.size(); i++)
    {
        cout << q[i] << endl;
    }
}

void main()
{
    deque d1;
    //添加到尾部
    d1.push_back(2);
    d1.push_back(10);
    //添加到頭部
    d1.push_front(-90);
    d1.push_front(-30); 

    //printDeque(d1);

    //cout << d1.front() << endl;
    //cout << d1.back() << endl;

    //兩個方向彈出
    //d1.pop_back();
    //d1.pop_front();

    printDeque(d1);

    //查找第一個-90元素索引位置,無需遍歷
    deque::iterator it = find(d1.begin(),d1.end(),-90);
    if (it != d1.end())
    {
        int idx = distance(d1.begin(), it);
        cout << "索引位置為:" << idx << endl;
    }


    system("pause");
}
//隊列(沒有迭代器)
/*
void main()
{
    queue q;
    q.push(78);
    q.push(18);
    q.push(20);
    q.push(33);

    //q.front();
    //q.back();
    while (!q.empty())
    {
        int tmp = q.front();
        cout << tmp << endl;
        q.pop();
    }   
    system("pause");
}
*/

//優先級隊列
/*
#include 

void main()
{
    //默認 最大值優先級
    priority_queue pq1;
    pq1.push(12);
    pq1.push(3);
    pq1.push(40);
    pq1.push(15);

    while (!pq1.empty())
    {
        int tmp = pq1.top();
        cout << tmp << endl;
        pq1.pop();
    }

    cout << "----------" << endl;
    //最小值優先級隊列
    priority_queue,greater> pq2;
    pq2.push(12);
    pq2.push(3);
    pq2.push(40);
    pq2.push(15);

    while (!pq2.empty())
    {
        int tmp = pq2.top();
        cout << tmp << endl;
        pq2.pop();
    }

    system("pause");
}
*/

4.棧stack

//棧
/*
#include 
void main()
{
    stack s;
    for (int i = 0; i < 10; i++)
    {
        s.push(i+1);
    }

    while (!s.empty())
    {
        int tmp = s.top();
        cout << tmp << endl;
        s.pop();
    }

    system("pause");
}
*/

5.集合list,set,map

//list
#include 
void printList(list& lst)
{
    //迭代器
    //沒有重載“<”運算符
    for (list::iterator it = lst.begin(); it != lst.end(); it++)
    {
        cout << *it << endl;
    }
}
//基本操作
/*
void main()
{
    list lst;
    for (int i = 0; i < 10; i++)
    {
        //尾部插入元素
        lst.push_back(i);
    }

    //頭部插入元素
    lst.push_front(80);
    lst.push_front(90);

    list::iterator it = lst.begin();
    it++;
    cout << *it << endl;
    //it = it + 3; 注意:不支持隨機訪問       

    printList(lst);

    system("pause");
}
*/


//刪除
/*
void main()
{
    list lst;
    for (int i = 0; i < 10; i++)
    {
        //尾部插入元素
        lst.push_back(i);
    }

    list::iterator it = lst.begin();
    //刪除
    it++;
    //刪除第二個元素
    //lst.erase(it);

    //刪除區間(已經被刪除了元素不能再刪除)
    list::iterator it_begin = lst.begin();
    list::iterator it_end = lst.begin();
    it_end++;
    it_end++;
    it_end++;
    lst.erase(it_begin, it_end);

    //直接根據內容刪除元素
    lst.remove(5);

    printList(lst);

    system("pause");
}
*/

//list插入(應用:頻繁的修改)
//vector(應用:隨機訪問v[100])
/*
void main()
{
    list lst;
    for (int i = 0; i < 10; i++)
    {
        //尾部插入元素
        lst.push_back(i);
    }

    list::iterator it = lst.begin();
    it++;
    lst.insert(it, 100);

    printList(lst);
    system("pause");
}
*/

//set 元素唯一 默認從小到大
#include 

void printSet(set &s)
{
    for (set::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << *it << endl;
    }
}
/*
void main()
{
    set s;
    //添加元素
    for (int i = 0; i < 10; i++)
    {
        s.insert(i+1);
    }
    s.insert(20);
    s.insert(15);
    s.insert(15);

    //刪除
    set::iterator it = s.begin();
    it++;
    s.erase(it);    

    printSet(s);
    system("pause");
}
*/

//元素按照從大到小排列
/*
#include 
void main()
{
    //同Java中:Map> 
    set> s;
    s.insert(10);
    s.insert(5);
    s.insert(20);
    s.insert(99);

    for (set>::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << *it << endl;
    }

    system("pause");
}
*/

//元素類型為Teacher對象,按照年齡排序
/*
class Teacher
{
public:
    Teacher(char* name, int age)
    {
        this->name = name;
        this->age = age;
    }

    void print()
    {
        cout << name << "," << age << endl;
    }   

public:
    char* name;
    int age;
};

//自定義排序規則
//仿函數
struct MyAgeSorter
{
    bool operator()(const Teacher &left, const Teacher &right)
    {
        return left.age < right.age;
    }
};

void main()
{
    set s;
    s.insert(Teacher("jack",18));
    s.insert(Teacher("rose", 20));
    s.insert(Teacher("jason", 22));
    s.insert(Teacher("alan", 5));
    //s.insert(Teacher("jimy", 5)); //不會插入

    for (set::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << (*it).name << "," << (*it).age << endl;
    }

    system("pause");
}
*/

//set查找
/*
void main()
{
    set s;
    //添加元素
    for (int i = 0; i < 10; i++)
    {
        s.insert(i + 1);
    }

    //printSet(s);

    //等於4的元素指針
    set::iterator s_4 = s.lower_bound(4); 
    //cout << *s_4 << endl;
    //大於4的元素指針
    set::iterator s_5 = s.upper_bound(4);
    //cout << *s_5 << endl;

    //一次性獲取等於4的元素指針,和大於4的元素指針\
    //BasicNameValuePair
    pair::iterator, set::iterator> p = s.equal_range(4);
    cout << *p.first << endl;
    cout << *p.second << endl;
    system("pause");
}
*/


//multiset 允許重復的元素
/*
void main()
{
    multiset s;
    s.insert(2);
    s.insert(8);
    s.insert(2);
    s.insert(8);

    for (multiset::iterator it = s.begin(); it != s.end(); it++)
    {
        cout <<  *it << endl;
    }

    system("pause");
}
*/


//map添加元素的方式
#include
#include 
/*
void main()
{
    //key -> value
    //1.
    map map1;
    map1.insert(pair(1,"jack"));
    map1.insert(pair(2, "rose"));

    //2
    map1.insert(make_pair(3,"jason"));

    //3
    map1.insert(map::value_type(4,"alan"));

    //4
    map1[5] = "jimmy"; //map["NO1"] = 90;

    //前三種方式,如果key已經存在,重復添加會報錯
    //第四種方式,如果key已經存在,重復添加會覆蓋

    //遍歷輸出
    for (map::iterator it = map1.begin(); it != map1.end(); it++)
    {
        cout << it->first << "," << it->second << endl;     
    }


    system("pause");
}

*/

void printMap(map &map1)
{
    for (map::iterator it = map1.begin(); it != map1.end(); it++)
    {
        cout << it->first << "," << it->second << endl;
    }
}

//刪除
/*
void main()
{
    map map1;
    map1.insert(pair(1, "jack"));
    map1.insert(pair(2, "rose"));
    map1.insert(pair(3, "jason")); 

    map::iterator it = map1.begin();
    it++;
    map1.erase(it);

    printMap(map1);

    system("pause");
}
*/

//添加的結果
/*
void main()
{
    map map1;
    map1.insert(pair(1, "jack"));
    map1.insert(pair(2, "rose"));
    map1.insert(pair(3, "jason"));
    //獲取添加的結果(first元素指針,second 是否成功)
    pair::iterator, bool> res = map1.insert(pair(3, "alan"));
    if (res.second)
    {
        cout << "添加成功" << endl;
    }
    else
    {
        cout << "添加失敗" << endl;
    }

    printMap(map1);

    system("pause");
}
*/


//查找
/*
void main()
{
    map map1;
    map1.insert(pair(1, "jack"));
    map1.insert(pair(2, "rose"));
    map1.insert(pair(3, "jason")); 

    printMap(map1);

    cout << "---------" << endl;

    //獲取key等於大於5的元素的值
    pair::iterator, map::iterator> p = map1.equal_range(2);
    if (p.first != map1.end()){
        //等於2的元素key value
        cout << p.first->first << p.first->second << endl;

        //大於2的元素key value
        cout << p.second->first << p.second->second << endl;
    }

    system("pause");
}
*/

//一個key對應多個value
//一個部門多個員工
//multimap
/*
class Employee
{
public:
    Employee(char* name,int age)
    {
        this->name = name;
        this->age = age;
    }

public:
    char* name;
    int age;
};

void main()
{
    multimap map1;

    //開發部
    map1.insert(make_pair("開發", Employee("擱淺", 20)));
    map1.insert(make_pair("開發", Employee("彪哥", 20)));

    //財務
    map1.insert(make_pair("財務", Employee("小穎", 16)));
    map1.insert(make_pair("財務", Employee("rose", 20)));

    //銷售
    map1.insert(make_pair("銷售", Employee("阿呆", 30)));
    map1.insert(make_pair("銷售", Employee("呵呵", 30)));

    //遍歷輸出
    for (multimap::iterator it = map1.begin(); it != map1.end(); it++)
    {
        cout << it->first << "," << it->second.name  << "," << it->second.age << endl;
    }

    cout << "----------------" << endl;
    //只獲取“財務”部的員工
    //獲取“財務部”員工的個數,key對應的value的個數
    int num = map1.count("財務");
    multimap::iterator it = map1.find("財務");
    int c = 0; //控制循環的次數
    while (it != map1.end() && c < num)
    {       
        cout << it->first << "," << it->second.name << "," << it->second.age << endl;
        it++;
        c++;
    }

    system("pause");
}
*/
//深拷貝與淺拷貝
/*
class Employee
{
public:
    //構造函數
    Employee(char* name, int age)
    {       
        this->name = new char[strlen(name) + 1];
        strcpy(this->name, name);
        this->age = age;
    }

    //析構函數
    ~Employee()
    {
        if (this->name != NULL)
        {
            delete[] this->name;
            this->name = NULL;
            this->age = 0;
        }
    }
    //拷貝構造函數
    //Employee e = 
    Employee(const Employee &obj)
    {
        this->name = new char[strlen(obj.name) + 1];
        strcpy(this->name, obj.name);
        this->age = obj.age;
    }

    //重載=
    //e1 = e2;
    Employee& operator=(const Employee &obj)
    {
        //釋放舊的內存
        if (this->name != NULL)
        {
            delete[] this->name;
            this->name = NULL;
            this->age = 0;
        }

        //重新分配
        this->name = new char[strlen(obj.name) + 1];
        strcpy(this->name, obj.name);
        this->age = obj.age;

        return *this;
    }

public: 
    char* name;
    int age;
};

void func()
{
    vector v1;
    Employee e1("jack",20); 
    v1.push_back(e1);
}

void main()
{
    //vector v1;
    //Employee e1("jack",20);
    //將e1拷貝到vector中
    //v1.push_back(e1);

    func();

    system("pause");
}
*/
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved