1. 程式人生 > >set和map增刪查改的使用

set和map增刪查改的使用

set的內部實現是基於紅黑樹,set就是以紅黑樹的格式儲存內部的所有元素的。
set中的所有元素都會根據元素的鍵值自動被排序。set的元素不像map那樣可以同時擁有實值(value)和鍵值(key)。set中只有一個值,set不允許兩個元素有相同的值。
我們不可以使用迭代器修改set的元素值,元素值已經按照紅黑樹的方式排列好,如果修改就會改變其組織。set中,可以允許刪除和增加一些元素。

map的內部實現同樣是基於紅黑樹。
和set不同,map的所有元素都是一個鍵值對pair(key,value),所有元素都會按照key的值進行排序,map中不允許有相同的key值,但可以修改map中元素的value值。
而multiset和multimap的特性和set和map一樣,唯一的差別是允許key重複
1 set和multiset


set和multiset會根據特定的排序準則,自動將元素進行排序。不同的是後者允許元素重複而前者不允許。
set和multiset兩種標頭檔案都為:#include<set>
set和multiset都是定義在std空間裡的類模板。

template < class T,                        // set::key_type/value_type
           class Compare = less<T>,        // set::key_compare/value_compare
           class Alloc = allocator<T>      // set::allocator_type
> class set;
template < class T,                        // multiset::key_type/value_type
           class Compare = less<T>,        // multiset::key_compare/value_compare
           class Alloc = allocator<T> >    // multiset::allocator_type
           > class multiset
;

其中第二個引數來定義排序準則為仿函式,預設為less,也就意味著,預設遍歷結果為升序。例:

    set<int,greater<int>> s1;
    s1.insert(10);
    s1.insert(3);
    s1.insert(9);
    s1.insert(8);
    s1.insert(1);
    s1.insert(3);

    set<int,greater<int>>::iterator it1 = s1.begin();
    while (it1 != s1.end())
    {
        cout << *it1 << "  ";
        ++it1;
    }
    cout << endl;

執行結果:
這裡寫圖片描述
相關操作
這裡寫圖片描述這裡寫圖片描述
1.1 插入
插入用insert函式

pair<iterator,bool> insert (const value_type& val);//pair::first set to an iterator pointing to either the newly inserted element or to the equivalent element already in the set. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent element already existed.
iterator insert (iterator position, const value_type& val);//在指定的位置插入指定的資料,position是一個迭代器的位置,x表示的是要插入的數。如果插入成功的話,會返回一個插入新位置的迭代器。
template <class InputIterator>
void insert (InputIterator first, InputIterator last);//插入一個迭代器區間

pair是一種模板模型,每個pair可以儲存兩個值,這倆個值得型別是任意型別。定義在#include中,其標準形式為:template< class T1,class T2> struct pair;
pair有兩個成員分別是first和second,set是一個模板,first是一個指向要插入位置的迭代器,如果second是false則first就是已經存在的元素的這個位置的迭代器,如果second為true,first就是要插入位置的迭代器。
結構如下:

template<class K,class V>
class pair
{
public:
    pair(const K& key, const V& v)
        :first(key)
        , second(v)
    {   }
    const K first;
    V second;
};

例:

set<int> myset;
set<int>::iterator it;
pair<iterator,bool> ret;
// 初始化
for (int i=1; i<=5; ++i) myset.insert(i*10);  
ret = myset.insert(20);  //20存在,插入失敗 
if (ret.second==false) it=ret.first;  // it指向20
myset.insert (it,25);    
myset.insert (it,24);          
myset.insert (it,26);       
int myints[]= {5,10,15};   
myset.insert (myints,myints+3);
for (it=myset.begin(); it!=myset.end(); ++it)
      cout << ' ' << *it;
cout << '\n';

1.2 查詢
查詢用find()函式

iterator find(const key_type& x) const;//若找到返回該迭代器的位置,否則就返回的是最後一個元素後面的位置

使用count()函式

size_type count(const key_type& x)const;//count是為了統計某個數值出現的次數,在set裡面不能重複出現相同的數值,count的返回值只有01兩個值,0表示的是不存在,1表示存在。

例:

set<int> s3;
s3.insert(10);
s3.insert(3);
s3.insert(9);
s3.insert(8);
s3.insert(1);
s3.insert(3);
set<int>::iterator pos = s3.find(10);//當迭代器沒找到返回end,end是最後一個數據的下一個
if (s3.count(8))
{
    cout << "exit" << endl;
}
else
{
    cout << "Non" << endl;
}

1.3 刪除

void erase(iterator position)//刪除一個迭代器位置
size_type erase(sonst key_type& x)//刪除成功的話就返回1
void erase(iterator first, iterator last)//刪除一段迭代器區間

例:刪除一個迭代器位置

set<int> s3;
s3.insert(10);
s3.insert(3);
s3.insert(9);
s3.insert(8);
s3.insert(1);
s3.insert(3);
set<int>::iterator pos = s3.find(10);//當迭代器沒找到返回end,end是最後一個數據的下一個
if (pos!=s3.end())
    s3.erase(pos);

例:刪除一段迭代器區間

set<int> s4;
set<int>::iterator itlow, itup;
for (int i = 1; i<10; i++) s4.insert(i * 10); // 10 20 30 40 50 60 70 80 90
itlow = s4.lower_bound(35);          
itup = s4.upper_bound(65);
s4.erase(itlow, itup);
set<int>::iterator it4 = s4.begin();
while (it4 != s4.end())
{
    cout << *it4 << "  ";
    ++it4;
}
cout << endl;

1.4 multiset在set的基礎上允許鍵值冗餘
例:

multiset<int> s4;
s4.insert(10);
s4.insert(3);
s4.insert(9);
s4.insert(8);
s4.insert(1);
s4.insert(3);
multiset<int>::iterator it4 = s4.begin();
while (it4 != s4.end())
{
    cout << *it4 << "  ";
    ++it4;
}
cout << endl;
multiset<int>::iterator pos = s4.find(3);
++pos;
cout << *pos << endl;
if (pos != s4.end())
    s4.erase(pos);
it4 = s4.begin();
while (it4 != s4.end())
{
    cout << *it4 << "  ";
    ++it4;
}
cout << endl;

執行結果:
這裡寫圖片描述
2 map和multimap
map和multimap兩種標頭檔案都為:#include<map>
map是一種key(鍵),value(值)的形式,用來儲存鍵和值組成的集合,鍵必須是唯一的,但值可以不唯一。裡面的元素可以根據鍵進行自動排序,由於map是key_value的形式,所以map裡的所有元素都是pair型別。pair裡面的first被稱為key(鍵),second被稱為value(值)。map的底層是用紅黑樹來實現的。
在剛開始介紹set的時候,簡單介紹了一下pair。現在將pair和make_pair再做一個詳細的補充吧
1)pair
pair是將兩個資料合成一個數據,當有這樣的需求時就要用到pair,就是map。還有一個運用就是函式要返回連個資料的時候要用到pair,pair是一個結構體,因為兩個成員變數用的是struct而不是class。
2)make_pair
可以使用pair的建構函式,也可以使用make_ pair來生成我們需要的pair。一般來說,在需要pair的時候我們用make_pair來生成pair的物件很方便,但由於pair可以進行隱式的型別轉換局帶來一些問題。

template<class K,class V>
inline pair<K, V> make_pair(const K& k,const V& v)
{
    return pair<K, V>(k, v);
}

相關操作
這裡寫圖片描述這裡寫圖片描述
例:

map<string, string> dict;
dict.insert(pair<string, string>("sort", "排序"));
dict.insert(pair<string, string>("insert", "插入"));
dict.insert(pair<string, string>("dict", "字典"));
map<string, string>::iterator it1 = dict.begin();
while (it1 != dict.end())
{
    //cout << (*it1).first << ":" << (*it1).second << endl;
    cout << it1->first << ":" << it1->second << endl;
    ++it1;
}
map<string, string>::iterator pos= dict.find("insert");
(*pos).second = "XXXXXXXXXXXXX";

2.1 插入insert()

pair<iterator,bool> insert (const value_type& val);//插入一個value_type型別,返回的則是一個pair型別。pair<iterator,bool>就是pair<map<K,V>::iterator,bool>。如果插入成功的話bool值就返回的是true,iterator指向插入的位置,否則的話iterator就指向已經存在的這個元素的位置,iterator是一個pair<K,V>型別的迭代器。
iterator insert (iterator position, const value_type& val);//在一個迭代器的位置插入一個value_type型別,插入成功的話就返回新插入位置的迭代器,否則就返回傳入的迭代器。
template <class InputIterator>
void insert (InputIterator first, InputIterator last);//插入的是一段迭代器區間

例:

map<string, string> dict;
dict.insert(pair<string, string>("sort", "排序"));    dict.insert(pair<string, string>("insert", "插入"));
dict.insert(pair<string, string>("dict", "字典"));
map<string, string>::iterator it1 = dict.begin();
while (it1 != dict.end())
{
    //cout << (*it1).first << ":" << (*it1).second << endl;
    cout << it1->first << ":" << it1->second << endl;
    ++it1;
}

2.2 查詢
find()函式

//查詢成功的話會返回這個元素的迭代器,否則返回的是end
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;

例:

map<string, string> dict;
dict.insert(pair<string, string>("sort", "排序"));
dict.insert(pair<string, string>("insert", "插入"));
dict.insert(pair<string, string>("dict", "字典"));
map<string, string>::iterator pos= dict.find("insert");
(*pos).second = "XXXXXXXXXXXXX";

count()函式

size_type count ( const key_type& x ) const;

和set中的count使用功能相同,count的返回值只有0和1兩個值,0表示的是不存在,1表示存在。
2.3 刪除

void erase ( iterator position );//刪除的是position位置的元素
size_type erase ( const key_type& x );//刪除的是鍵值所在的位置,成功為1,否則為0
void erase ( iterator first, iterator last );//刪除一段迭代器區間

例:

map<string, string>::iterator pos= dict.find("insert");
if (pos != dict.end())
{
    dict.erase(pos);
}

2.4 operator[]
可以實現修改和插入

mapped_type& operator[] (const key_type& k);

這裡寫圖片描述
operator[]是很常用的,如果map中有這個key,則它就把這個key所對應的value的引用返回。如果map中沒有這個key的話,則它會呼叫insert(pair< K ,V > (k,V())),將k和V的預設值對應起來插入後並返回這個value的引用。
其實現如下:

mapped_type& operator[] (const key_type& k)
{
     return (*((this->insert(make_pair(k,mapped_type()))).first)).second;
}

例:

string strs[] = { "sort", "sort", "sort", "insert", "insert","second", "left" };
map<string, size_t> countMap; 
for (size_t i = 0; i < sizeof(strs) / sizeof(string); ++i)
{
    //方法1
    map<string, size_t>::iterator it = countMap.find(strs[i]);
    if (it != countMap.end())
    {
        it->second++;
    }
    else
    {
        //countMap.insert(pair<string, size_t>(strs[i], 1));
        countMap.insert(make_pair(strs[i], 1));
    }
    //方法2
    pair<map<string, size_t>::iterator, bool> ret = countMap.insert(make_pair(strs[i], 1));
    if (ret.second == false)
    {
        ret.first->second++;
    }
    //方法3
    countMap[strs[i]]++;//[]的返回值為value的引用
}
map<string, size_t>::iterator it1 = countMap.begin();
while (it1 != countMap.end())
{
    cout << it1->first << ":" << it1->second << endl;
    it1++;
}   
cout << endl;

執行結果:
這裡寫圖片描述
例:

map<string, string> dict;
dict["sort"] = "排序";//用[],其中value必須要有預設的建構函式
dict["insert"] = "字串";
dict["left"];
dict["left"]="剩餘";
dict["left"] = "左邊";
dict.insert(make_pair("first", "XXXXXX"));
dict.insert(make_pair("first", "YYYYYYYYYY"));
map<string, string>::iterator it1 = dict.begin();
while (it1 != dict.end())
{
    cout << it1->first << ":" << it1->second << endl;
    it1++;
}   
cout << endl;

這裡寫圖片描述

3 map和set模擬實現程式碼
map和set的底層為紅黑樹,故而要藉助紅黑樹

//RBtree.h
#pragma once 
#include<utility>//包含pair型別
enum Colour
{
    RED,
    BLACK,
};

template<class V>
struct RBTreeNode
{
    RBTreeNode<V>* _left;
    RBTreeNode<V>* _right;
    RBTreeNode<V>* _parent;

    V _value;
    Colour _col;
    RBTreeNode(const V& value = V())
        :_value(value)
        , _left(NULL)
        , _right(NULL)
        , _parent(NULL)
        , _col(RED)//初始化插入節點的顏色為紅色,不影響黑節點個數 
    {}
};

//迭代器
template<class V, class Ref, class Ptr>
struct _RBTreeIterator
{
    typedef RBTreeNode<V> Node;
    typedef _RBTreeIterator<V, Ref, Ptr> Self;
    Node* _node;

    _RBTreeIterator()
    {}
    _RBTreeIterator(Node* node)
        :_node(node)
    {}

    Ref operator *()
    {
        return _node->_value;
    }
    Ptr operator ->()
    {
        return &(operator *());
    }
    Self operator ++()
    {
        if (_node->_right)
        {
            Node* subleft = _node->_right;
            while (subleft->_left)
            {
                subleft = subleft->_left;
            }
            _node = subleft;
        }
        else
        {
            Node* Parent = _node->_parent;
            Node* cur = _node;
            while (Parent&&cur == Parent->_right)
            {
                cur = Parent;
                Parent = Parent->_parent;
            }
            _node = Parent;
        }
        return *this;
    }

    Self operator ++(int)
    {
        Self it(*this);
        ++(*this);
        return it;
    }
    Self operator --()
    {
        if (_node->_left)
        {
            Node* subright = _node->_left;
            while (subright->_right)
            {
                subright = subright->_right;
            }
            _node = subright;
        }
        else
        {
            Node* Parent = _node->_parent;
            Node* cur = _node;
            while(Parent&&cur == Parent->_left)
            {
                cur = Parent;
                Parent = Parent->_parent;
            }
            _node = Parent;
        }
        return *this;
    }
    Self operator --(int)
    {
        Self it(*this);
        --(*this);
        return *this;
    }
    bool operator !=(const Self& s)
    {
        return _node != s._node;
    }

};

//my_set->rbtree<K,K>
//my_map->rbtree<K,pair<K,V>)

//template<class K, class V>
//class pair
//{
//public:
//  pair(const K& key, const V& v)
//      :first(key)
//      ,second(v)
//  {}
//
//  const K first;
//  V second;
//};
//
//template<class K, class V>
//inline pair<K, V> make_pair(const K& k, const V& v)
//{
//  return pair<K, V>(k, v);
//}

template<class K, class V, class KeyOfValue>
struct RBTree
{
    typedef RBTreeNode<V> Node;
public:
    typedef _RBTreeIterator<V, V&, V*> Iterator;
    typedef _RBTreeIterator<V, const V&, const V*> ConstIterator;
    RBTree()
        :_root(NULL)
    {}
    Iterator Begin()
    {
        Node* left = _root;
        while (left->_left)
        {
            left = left->_left;
        }
        return left;
    }
    Iterator End()
    {
        return NULL;
    }
    std::pair<Iterator,bool> Insert(const V& value)
    {
        if (_root == NULL)
        {
            _root = new Node(value);
            _root->_col = BLACK;
            return make_pair(Iterator(_root),true);
        }
        KeyOfValue kofv;
        Node* parent = NULL;
        Node* cur = _root;
        while (cur)
        {
            if (kofv(cur->_value) > kofv(value))//三叉鏈
            {
                parent = cur;
                cur = cur->_left;
            }
            else if (kofv(cur->_value) < kofv(value))
            {
                parent = cur;
                cur = cur->_right;
            }
            else
            {
                return make_pair(Iterator(cur),false);
            }
        }
        cur = new Node(value);
        Node* ret = cur;
        if (kofv(parent->_value) < kofv(value))
        {
            parent->_right = cur;
            cur->_parent = parent;
        }
        else
        {
            parent->_left = cur;
            cur->_parent = parent;
        }
        // 調平衡 
        //檢查規則:1.parent為黑,不用調整
        //2.parent為紅,uncle為紅
        //3.parent為紅,uncle為黑或者不存在
        while (parent && parent->_col == RED)
        {
            Node* grandfather = parent->_parent;
            if (parent == grandfather->_left)
            {
                Node* uncle = grandfather->_right;
                if (uncle&&uncle->_col == RED)//parent為紅,uncle為紅
                {
                    parent->_col = uncle->_col = BLACK;
                    grandfather->_col = RED;

                    cur = grandfather;
                    parent = cur->_parent;
                }
                else//parent為紅,uncle為黑或者不存在,分單旋和雙旋兩種情況
                {
                    if (cur == parent->_right)//雙旋
                    {
                        RotateL(parent);
                        swap(cur, parent);
                    }
                    RotateR(grandfather);
                    parent->_col = BLACK;
                    grandfather->_col = RED;
                }
            }
            else
            {
                Node* uncle = grandfather->_left;
                if (uncle&&uncle->_col == RED)//parent為紅,uncle為紅
                {
                    parent->_col = uncle->_col = BLACK;
                    grandfather->_col = RED;

                    cur = grandfather;
                    parent = cur->_parent;
                }
                else//parent為紅,uncle為黑或者不存在,分單旋和雙旋兩種情況
                {
                    if (cur == parent->_left)//雙旋
                    {
                        RotateR(parent);
                        swap(cur, parent);
                    }
                    RotateL(grandfather);
                    parent->_col = BLACK;
                    grandfather->_col = RED;
                }
            }
        }
        _root->_col = BLACK;
        return make_pair(Iterator(ret),true);
    }

    void RotateR(Node* parent)//右旋
    {
        Node* subL = parent->_left;
        Node* subLR = subL->_right;
        parent->_left = subLR;
        if (subLR)
        {
            subLR->_parent = parent;
        }
        Node* PPnode = parent->_parent;
        subL->_right = parent;
        parent->_parent = subL;
        if (parent == _root)
        {
            _root = subL;
        }
        else
        {
            if (PPnode->_left == parent)
            {
                PPnode->_left = subL;
            }
            else
            {
                PPnode->_right = subL;
            }
            subL->_parent = PPnode;
        }
        _root->_parent = NULL;
    }


    void RotateL(Node* parent)//左旋
    {
        Node* subR = parent->_right;
        Node* subRL = subR->_left;
        parent->_right = subRL;
        if (subRL)
        {
            subRL->_parent = parent;
        }
        Node* PPnode = parent->_parent;
        subR->_left = parent;
        parent->_parent = subR;
        if (parent == _root)
        {
            _root = subR;
            _root->_parent = NULL;
        }
        else
        {
            if (PPnode->_left == parent)
            {
                PPnode->_left = subR;
                subR->_parent = PPnode;
            }
            else
            {
                PPnode->_right = subR;
                subR->_parent = PPnode;
            }
        }
    }


    bool _IsBalance(Node* root, const int & count, int num)
    {
        if (root->_col == NULL)
            return true;
        if (root->_col == RED&&root->_parent->_col == RED)//存在兩個連續的紅節點 
            return false;
        if (root->_col == BLACK)//黑節點就CurBlackNum++  
            num++;
        if (root->_left == NULL&&root->_right == NULL)
        {
            if (num == count)
            {
                return true;
            }
            else
            {
                return false;//路徑上黑節點個數不同返回false
            }
        }
        return _IsBalance(root->_left, count, num) && _IsBalance(root->_right, count, num);
    }
    bool IsBalance()//是否為紅黑樹
    {
        if (_root->_col != BLACK)
        {
            return false;
        }
        int count = 0;//統計出一條路徑的黑色節點的個數  
        int num = 0;//需要與count比較的其他路徑黑色節點個數 
        Node* cur = _root;
        while (cur)
        {
            if (cur->_col == BLACK)
            {
                count++;
            }
            cur = cur->_right;
        }
        return _IsBalance(_root, count, num);
    }

    void _InOrder(Node* root)
    {
        if (root == NULL)
        {
            return;
        }
        _InOrder(root->_left);
        //  cout << kofv(root->_value) << "  ";
        _InOrder(root->_right);
    }
    void InOrder()
    {
        _InOrder(_root);
        cout << endl;
    }
    Iterator Find(const K& key)
    {
        KeyOfValue kofv;
        Node* cur = _root;
        while (cur)
        {
            if (kofv(cur->_value) > key)//三叉鏈
            {
                cur = cur->_left;
            }
            else if (kofv(cur->_value) < key)
            {
                cur = cur->_right;
            }
            else
            {
                return Iterator(cur);
            }
        }
        return End();
    }

private:
    Node* _root;
};


struct SetKeyOfValue
{
    const int& operator()(const int& key)
    {
        return key;
    }
};
//Myset.h
#pragma once
#include"RBtree.h"
#include<utility>//包含pair型別
#include<string>

template<typename K>
class Set
{
public:
    struct SetKeyOfValue
    {
        const K& operator()(const K& key)
        {
            return key;
        }
    };
    typedef typename RBTree<K, K, SetKeyOfValue>::Iterator Iterator;
    std::pair<Iterator, bool> Insert(const K& key)
    {
        return _t.Insert(key);
    }
    Iterator Find(const K& key)
    {
        return _t.Find(key);
    }
    Iterator Begin()
    {
        return _t.Begin();
    }
    Iterator End()
    {
        return _t.End();
    }

protected:
    RBTree<K, K, SetKeyOfValue> _t;
};
//Mymap.h
#pragma once
#include<utility>//包含pair型別
#include"RBtree.h"

#include<string>

template<class K, class V>
class Map
{
public:
    struct MapKeyOfValue
    {
        const K& operator()(const std::pair<K, V>& kv)
        {
            return kv.first;
        }
    };
    typedef typename RBTree<K, std::pair<K, V>, MapKeyOfValue>::Iterator Iterator;
    std::pair<Iterator, bool> Insert(std::pair<K, V>& kv)
    {
        return _t.Insert(kv);
    }
    V& operator [](const K& key)
    {
        pair<Iterator, bool> ret = Insert(make_pair(key, V()));
        return (ret.first)->second;
    }
    Iterator Find(const K& key)
    {
        return _t.Find(key);
    }
    Iterator Begin()
    {
        return _t.Begin();
    }
    Iterator End()
    {
        return _t.End();
    }
protected:

    RBTree<K, std::pair<K, V>, MapKeyOfValue> _t;

};
//test.cpp
#include<utility>//包含pair型別
#include"RBtree.h"
#include"Mymap.h"
#include"Myset.h"
#include<iostream>
#include"Windows.h"

using namespace std;

void TestRBTree()
{
    int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
    RBTree<int, int, SetKeyOfValue> t;
    for (size_t i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
    {
        t.Insert(a[i]);
    }
    RBTree<int, int, SetKeyOfValue>::Iterator it = t.Begin();
    while (it != t.End())
    {
        cout << *it << " ";
        it++;
    }
    cout << endl;
    it = t.Begin();
    ++it;
    ++it;
    ++it;
    --it;
    --it;
    cout << *it << endl;
}

void TestMySet()
{
    Set<string> s;
    s.Insert("sort");
    s.Insert("insert");
    s.Insert("set");
    s.Insert("map");
    s.Insert("iterator");
    s.Insert("value");
    s.Insert("value");
    Set<string>::Iterator it1 = s.Begin();
    while (it1 != s.End())
    {
        cout << *it1 << " ";
        ++it1;
    }
    cout << endl;
}
void TestMyMap()
{
    Map<string, string> dict;
    dict.Insert(pair<string, string>("left", "左邊"));
    dict.Insert(pair<string, string>("dict", "字典"));
    dict.Insert(pair<string, string>("Insert", "字串"));
    dict.Insert(pair<string, string>("map", "對映"));
    dict.Insert(pair<string, string>(
            
           

相關推薦

setmap增刪的使用

set的內部實現是基於紅黑樹,set就是以紅黑樹的格式儲存內部的所有元素的。 set中的所有元素都會根據元素的鍵值自動被排序。set的元素不像map那樣可以同時擁有實值(value)和鍵值(key)。set中只有一個值,set不允許兩個元素有相同的值。 我們

mapset增刪

一: map迭代器區間的範圍:  [通常是左閉右開)二:multimapmultimap跟map大多數介面基本都相同,只不過multimap是可以在map中插入相同的key值底層紅黑樹就是把相同元素直接鏈到相同元素的letf或right上    二:setset的話,就是ma

總結mapset增刪的使用

在STL中map和set都是關聯式容器,vector和list是序列式容器,在今天的這篇文章中主要介紹的是map和set的基本用法。 一、set set是一種key型別的集合結構,所以set是不允許有重複元素的一種結構,set中所有元素的key值都會被自動排序為升序。set和

mapset增刪

 //2.1 way one for(map<int,int>::iterator it=numCountMap.begin() ;it!=numCountMap.end();it++) { cout<<it->first<<" occ

setmap增刪

一,set用法介紹: set是單詞set(集合)的意思 只有一個鍵值,set當中不能儲存相同的鍵值 所有的元素都會根據元素的鍵值自動被排序。(底層檢索使用中序遍歷演算法) 相關函式: (1)begin:返回一個迭代器,此迭代器指向set中到的第

SSH框架的多表查詢增刪 (方法一)上

ips 查詢 href ssh margin blank 麻煩 tle 指點 原創作品,允許轉載,轉載時請務必標明作者信息和聲明本文章==》 http://www.cnblogs.com/zhu520/p/7772823.html 因為最近在做Android 練習

SSH學習02 【SSH網上商城專案實戰02】基本增刪、ServiceAction的抽取以及使用註解替換xml

【SSH網上商城專案實戰02】基本增刪查改、Service和Action的抽取以及使用註解替換xml 轉自:https://blog.csdn.net/eson_15/article/details/51297698 上一節我們搭建好了Struts2、Hibernate和Spring的開

分頁外掛,反向生成工程進行增刪操作

反向生成工程可以直接通過資料庫表生成對應的實體類和dao層mapper配置檔案 在反向生成的專案配置檔案中修改以下幾點   1> <!--資料庫連線的資訊:驅動類、連線地址、使用者名稱、密碼 -->        &nbs

使用EntityFramework6完成增刪事務

使用EF對資料庫進行操作,整個過程就像運算元組一樣,我們只管修改或向集合中新增值,最後通知EF儲存修改後的結果就可以了。 準備工作 為了演示,我在資料庫中建了兩張表。class表用於表示班級,class_id是班級編號,class_name是班級名稱。第二張表是學生表student,studen

2017-12-19python全棧9期第四天第二節之列表的增刪之刪除的popdelremoveclear

Python全棧 python use 刪除 rem pri utf-8 int 返回 #!/user/bin/python# -*- coding:utf-8 -*-li = [‘zs‘,‘ls‘,‘ww‘,‘zl‘]# name = li.pop(1) #按索引位置刪除

2017-12-20python全棧9期第五天第二節之字典的增刪字典的for迴圈

#!/user/bin/python# -*- coding:utf-8 -*-dic1 = {'age':24,'name':'zd','sex':'boy'}print(dic1)#增dic1['high'] = 175 #沒有建就新增dic1['sex'] = 'boy1' #有建就修改print(di

基於ES6原生nodejs實現自定義路由,靜態檔案伺服器增刪的MVC架構分享

基於ES6和原生nodejs來實現一個基於MVC的增刪查改功能示例分享 自定義路由的解耦實現 首先分別處理不同方式的請求: const http = require('http'); const url = require('url')

簡單的用c3p0dbutils實現的資料庫增刪

import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.d

用C語言實現順序表的增刪逆置

在資料結構中,我們最開始接觸的就是順序表,那麼順序表是什麼呢?順序表是在計算機記憶體中以陣列的形式儲存的線性表,是指用一組地址連續的儲存單元依次儲存資料元素的線性結構。線性表採用順序儲存的方式儲存就稱之為順序表。順序表是將表中的結點依次存放在計算機記憶體中一組地

c++ map使用(增刪遍歷)

本文實現數字計數並介紹map的增刪查改遍歷實現. 首先上增刪查改遍歷的程式碼 #include <iostream> #include<map> #include<set> using namespace std; i

在MVC程式中,使用泛型倉儲模式工作單元實現增刪

在這片文章中,我將自己動手為所有的實體:寫一個泛型倉儲類,還有一個工作單元。 工作單元的職責就是:為每一個實體,建立倉儲例項。倉儲(倉庫)的職責:增刪查改的功能實現。 我們將會在控制器中,建立工作單元類(UnitOfWork)的例項,然後根據實體,建立倉儲例項,再就是

HDFS Java Client對hdfs文件增刪

apache pom.xml onf != open readline inpu test .get step1:增加依賴 pom.xml ... <!-- https://mvnrepository.com/artifact/org

CI 增刪

用戶名 信息 nbsp 復制 arr 數組 pan res 關聯數組 增: //insert//語法:$bool=$this->db->insert(‘表名‘,關聯數組); $data=array( ‘username‘=>‘mary‘,

11.Set Map數據結構

console each 表示 數組 weak 操作方法 cto delet 用法 1.set 基本用法 ES6 提供了新的數據結構 Set。它類似於數組,但是成員的值都是唯一的,沒有重復的值。 Set 本身是一個構造函數,用來生成 Set 數據結構。

php mysql增刪

設置 匹配 values bsp update 判斷 數字 可用 字數 php mysql增刪查改代碼段 $conn=mysql_connect(‘localhost‘,‘root‘,‘root‘); //連接數據庫代碼 mysql_query("set names ut