C++语法:STL容器与位运算

本文深入探讨了C++中的STL容器,包括vector、queue、deque、stack、set和map,并介绍了位运算的基本概念和常用操作,如与、或、非、异或及移位。同时,提供了多个例题,如查找数字在排序数组中的出现次数、调整数组顺序等,以助于读者理解和应用这些概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++语法:STL容器与位运算


一. STL容器

**vector **
变长数组

queue
队列

deque
双端队列

stack

set
有序集合

map
映射(类似于数组用其他结构)

注意
“前闭后开”的形式

在这里插入图片描述

二. 位运算

基本概念
& 与
| 或
~ 非
^ 异或
>> 右移(看剩下的,相当于处二的K次)
<< 左移(看出来的,相当于乘二的K次)

常用操作

(1) 求x的第k位数字 , x >> k & 1
(2) 返回x的最后一位1 , lowbit(x) = x & -x

三.例题求解

课内例题
1.数字在排序数组中出现的次数

class Solution {
public:
    int getNumberOfK(vector<int>& nums, int k) {
        int cnt = 0;
        for (int x : nums)
            if (x == k)
                cnt ++ ;
        return cnt;
    }
};

2.0到n-1中缺失的数字

class Solution {
public:
    int getMissingNumber(vector<int>& nums) {
        if (nums.empty()) return 0;

        int l = 0, r = nums.size() - 1;
        while (l < r)
        {
            int mid = l + r >> 1;
            if (nums[mid] != mid) r = mid;
            else l = mid + 1;
        }

        if (nums[r] == r) r ++ ;
        return r;
    }
};

**3.调整数组顺序使奇数位于偶数前面 **

class Solution {
public:
    void reOrderArray(vector<int> &array) {
         int l = 0, r = array.size() - 1;
         while (l < r) {
             while (l < r && array[l] % 2 == 1) l ++ ;
             while (l < r && array[r] % 2 == 0) r -- ;
             if (l < r) swap(array[l], array[r]);
         }
    }
};

**4.从尾到头打印链表 **

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> printListReversingly(ListNode* head) {
        vector<int> res;
        while (head) {
            res.push_back(head->val);
            head = head->next;
        }
        return vector<int>(res.rbegin(), res.rend());
    }
};

5.用两个栈实现队列

class MyQueue {
public:
    /** Initialize your data structure here. */
    stack<int> stk, cache;
    MyQueue() {

    }

    /** Push element x to the back of queue. */
    void push(int x) {
        stk.push(x);
    }

    void copy(stack<int> &a, stack<int> &b) {
        while (a.size()) {
            b.push(a.top());
            a.pop();
        }
    }

    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        copy(stk, cache);
        int res = cache.top();
        cache.pop();
        copy(cache, stk);
        return res;
    }

    /** Get the front element. */
    int peek() {
        copy(stk, cache);
        int res = cache.top();
        copy(cache, stk);
        return res;
    }

    /** Returns whether the queue is empty. */
    bool empty() {
        return stk.empty();
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * bool param_4 = obj.empty();
 */

课间习题
1.最小的k个数

class Solution {
public:
    vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
        sort(input.begin(), input.end());

        vector<int> res;
        for (int i = 0; i < k; i ++ ) res.push_back(input[i]);

        return res;
    }
};

2.和为S的两个数字

class Solution {
public:
    vector<int> findNumbersWithSum(vector<int>& nums, int target) {
        unordered_set<int> S;
        for (auto x : nums)
        {
            if (S.count(target - x)) return {x, target - x};
            S.insert(x);
        }
    }
};

**3.数字排列 **


class Solution {
public:
    vector<vector<int>> permutation(vector<int>& nums) {
        sort(nums.begin(), nums.end());

        vector<vector<int>> res;
        do res.push_back(nums); while (next_permutation(nums.begin(), nums.end()));

        return res;
    }
};

4.二进制中1的个数


class Solution {
public:
    int NumberOf1(int n) {
        int res = 0;
        unsigned int un = n; 
        while (un) res += un & 1, un >>= 1;
        return res;
    }
};

5.三元组排序

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10010;

struct Data
{
    int x;
    double y;
    string z;

    bool operator< (const Data &t) const
    {
        return x < t.x;
    }
}a[N];

int main()
{
    int n;
    cin >> n;

    for (int i = 0; i < n; i ++ ) cin >> a[i].x >> a[i].y >> a[i].z;

    sort(a, a + n);

    for (int i = 0; i < n; i ++ )
        printf("%d %.2lf %s\n", a[i].x, a[i].y, a[i].z.c_str());

    return 0;
}

学习资源 《语法基础》