C++备忘录

C++备忘录,第1张

C++备忘录 C++备忘录

github

二维数组
#include
using namespace std;



int main() {

	// 已知大小
	int arr_1[2][3] = { {1, 2, 3}, {4, 5, 6} };

	// 未知大小
	//    创建
	int m = 2, n = 3;
	int** arr_2 = new int*[m];
	for (int i = 0; i < m; i++) {
		arr_2[i] = new int[n];
	}
	//    删除
	for (int i = 0; i < m; i++) {
		delete[] arr_2[i];
	}
	delete[] arr_2;

	return 1;
}
STL container
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
#include 

using namespace std;



int main() {

    // map
    map mmap;
    mmap.insert(make_pair(1,10));
    mmap.insert(make_pair(2,20));
    mmap.insert(make_pair(3,30));
    mmap.insert(make_pair(4,40));
    mmap.insert(make_pair(5,50));
    for(auto item: mmap) cout << item.first << " " << item.second << endl;
    map::iterator iter_low = mmap.lower_bound(2);
    map::iterator iter_up = mmap.upper_bound(4);
    for(map::iterator iter=iter_low; iter!=iter_up; iter++) {
        cout << (*iter).first << " " << (*iter).second << endl;
    }
    cout << mmap.count(1) << endl;
    cout << (*mmap.find(1)).first << " " << (*mmap.find(1)).second << endl;
    mmap.erase(1);
    cout << mmap.count(1) << endl;
    mmap[2] += 1;
    for(auto item: mmap) cout << item.first << " " << item.second << endl;
    mmap.erase(next(mmap.begin()), prev(mmap.end()));
    for(auto item: mmap) cout << item.first << " " << item.second << endl;

    // set
    vector mvec = {0, 1, 2, 3, 3};
    set mset(mvec.begin(), mvec.end());
    for(auto item: mset) cout << item << " ";
    cout << endl;
    mvec.assign(mset.begin(), mset.end());
    for(auto item: mvec) cout << item << " ";
    cout << endl;

    // priority_queue
    priority_queue, greater> pq;
    pq.push(2);
    pq.push(1);
    pq.push(3);
    while(!pq.empty()) {
        cout << pq.top() << " ";
        pq.pop();
    }
    cout << endl;

    // deque
    deque dq;
    dq.push_back(1);
    dq.push_back(2);
    dq.push_back(3);
    cout <<  dq.front() << " " << dq.back() << endl;
    dq.pop_back();
    dq.push_front(1);
    dq.pop_front();
    

    return -1;
}
STL algorithm
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
#include 

using namespace std;



void print_vector(vector vec) {
    vector::iterator iter;
    for(iter=vec.begin(); iter!=vec.end(); iter++) {
        cout << *iter << " ";
    }
    cout << endl;
}

bool is_six(int i) {
    return i==6;
}

void plus_two(int& i) {
    i += 2;
}

int get_one() {
    return 1;
}

bool cmp(int a, int b) {
    return a > b; 
}

int main() {
    int m = 2, n = 3;

    // 二维
    vector> vec(m, vector(n, 0));
	vector> vec_2(vec.begin() + 1, vec.end());
	cout << vec_2.size() << " " << vec_2[0].size() << endl;

    // api
    vector vec_1(10, 0);
    print_vector(vec_1);

    vec_1.assign(7, 4);
    print_vector(vec_1);
    cout << vec_1.size() << endl;

    vec_1.push_back(1);
    vec_1.pop_back();

    int& tmp = vec_1.front();
    cout << tmp << endl;
    tmp = vec_1.back();
    cout << tmp << endl;

    vec_1.insert(vec_1.begin()+1, 3, 20);
    print_vector(vec_1);

    vec_1.erase(vec_1.begin());
    print_vector(vec_1);
    vec_1.erase(vec_1.begin(), vec_1.begin()+2);
    print_vector(vec_1);

    // copy(first, last, dst_first)
    vector dst(10);
    copy(vec_1.begin(), vec_1.end(), dst.begin()+2);
    print_vector(dst);

    // count(first, last, val)
    cout << count(vec_1.begin(), vec_1.end(), 4) << endl;
    cout << count(vec_1.begin(), vec_1.end(), 30) << endl;

    // equal(v1.begin, v1.end, v2.begin, v2.end)
    vector vec_3(vec_1);
    cout << equal(vec_1.begin(), vec_1.end(), vec_3.begin(), vec_3.end()) << endl;
    vec_3.pop_back();
    cout << equal(vec_1.begin(), vec_1.end(), vec_3.begin(), vec_3.end()) << endl;

    int t = 4;
    // cout << equal_range(vec_1.begin(), vec_1.end()) << endl;

    // fill(first, last, val)
    fill(vec_1.begin(), vec_1.end(), 10);
    print_vector(vec_1);

    // find(first, last, val)
    for(int i=0; i());
    print_vector(vec_1);
    make_heap(vec_1.begin(), vec_1.end(), greater());
    print_vector(vec_1);
    sort_heap(vec_1.begin(), vec_1.end(), cmp);
    print_vector(vec_1);

    vector vec_4(5, 8);
    fill(vec_4.begin(), vec_4.begin()+3, 4);
    vector::iterator vec_4_iter = unique(vec_4.begin(), vec_4.end());
    for(auto iter=vec_4.begin(); iter!=vec_4_iter; iter++) cout << *iter << " ";
    cout << endl;

    print_vector(vec_1);
    replace(vec_1.begin(), vec_1.end(), 6, 10);
    print_vector(vec_1);

    auto remove_iter = remove(vec_1.begin(), vec_1.end(), 10);
    print_vector(vec_1);
    vec_1.erase(remove_iter, vec_1.end());
    print_vector(vec_1);

    return -1;
}
string
#include
#include
#include
#include

using namespace std;

int main() {
    string s = "abcde";

    string s_s = s.substr(1, 3);
    cout << s_s << endl;

    int idx = s.find("da");
    cout << idx << endl;

    string::size_type idx_1 = s.find('q');
    cout << idx << endl;
    cout << (idx==string::npos) << endl;
    cout << (idx==-1) << endl;

    s.append("w");
    s.insert(0, "2");
    cout << s.front() << s.back() << stoi(s) << s.compare("21") << s.length() << s.size() << endl;

    string s_2 = "qwert";
    s += s_2;
    cout << s;
    s.erase(0, 3);
    cout << endl << s << endl;
    return -1;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5698365.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存