/* ============================================================================ Name : seq_list_vector.cpp Author : ntsk13 [email protected] Version : Copyright : GPL Description : sequence list study, complement by C++ vector Date : 2015.06.08 ============================================================================ */ #include <iostream> #include<vector> #include<algorithm> using namespace std; typedef struct elem { int element; }Seqlist_t; class sqlist { public: vector<Seqlist_t> v; int len;//长度 void init(); void clear(); bool is_empty(); int length(); Seqlist_t get_elem(int i); int get_locate(Seqlist_t e); Seqlist_t get_last(int i); Seqlist_t get_next(int i); void add_back(Seqlist_t e); void delete_back(); void insert_elem(Seqlist_t e,int i); void delete_elem(int i); void traverse(); //void sort_sql(); }; int main() { //int a[5]={3,0,4,1,2}; sqlist L; Seqlist_t zero,one,two,three,four; zero.element=3; one.element=0; two.element=4; three.element=1; four.element=2; L.init(); L.add_back(zero); L.add_back(one); L.add_back(two); L.add_back(three); L.add_back(four); L.traverse(); cout<<"===================================="<<endl; L.delete_back(); L.traverse(); cout<<"===================================="<<endl; L.delete_elem(2); L.traverse(); cout<<"===================================="<<endl; L.insert_elem(four,0); L.traverse(); cout<<"===================================="<<endl; return 0; } void sqlist::init() { len=0; } void sqlist::clear() { v.clear(); len=0; } bool sqlist::is_empty() { return (len==0)?true:false; } int sqlist::length() { return len; } Seqlist_t sqlist::get_elem(int i) { return v.at(i); } int sqlist::get_locate(Seqlist_t e) { int i=0; for( i=0; i< len;i++) if(e.element==v.at(i).element) break; return (i !=len ) ? (i) : (-1); } Seqlist_t sqlist::get_last(int i) { return v.at(i-1); } Seqlist_t sqlist::get_next(int i) { return v.at(i+1); } void sqlist::add_back(Seqlist_t e) { v.push_back(e); len++; } void sqlist::delete_back() { v.pop_back(); len--; } void sqlist::insert_elem(Seqlist_t e,int i) { v.insert(v.begin()+ i,e); len++; } void sqlist::delete_elem(int i) { v.erase(v.begin()+ i); len--; } void sqlist::traverse() { for(int i=0;i<len;i++) cout<<"The element is "<< v.at(i).element<<endl; ; } <pre name="code" class="cpp">/* ============================================================================ Name : seq_list_vector.cpp Author : ntsk13 [email protected] Version : Copyright : GPL Description : sequence list study, complement by C++ vector Date : 2015.06.08 ============================================================================ */ #include <iostream> #include<vector> #include<algorithm> using namespace std; class sqlist { public: vector<int> v; int len;//长度 void init(); void clear(); bool is_empty(); int length(); int get_elem(int i); int get_locate(int e); int get_last(int i); int get_next(int i); void add_back(int e); void delete_back(); void insert_elem(int e,int i); void delete_elem(int i); void traverse(); void sort_sql(); }; int main() { //int a[5]={3,0,4,1,2}; sqlist L; int zero,one,two,three,four; zero=3; one=0; two=4; three=1; four=2; L.init(); L.add_back(zero); L.add_back(one); L.add_back(two); L.add_back(three); L.add_back(four); L.traverse(); cout<<"===================================="<<endl; L.delete_back(); L.traverse(); cout<<"===================================="<<endl; L.delete_elem(2); L.traverse(); cout<<"===================================="<<endl; L.insert_elem(four,0); L.traverse(); cout<<"===================================="<<endl; L.sort_sql(); L.traverse(); return 0; } void sqlist::init() { len=0; } void sqlist::clear() { v.clear(); len=0; } bool sqlist::is_empty() { return (len==0)?true:false; } int sqlist::length() { return len; } int sqlist::get_elem(int i) { return v.at(i); } int sqlist::get_locate(int e) { int i=0; for( i=0; i< len;i++) if(e==v.at(i)) break; return (i !=len ) ? (i) : (-1); } int sqlist::get_last(int i) { return v.at(i-1); } int sqlist::get_next(int i) { return v.at(i+1); } void sqlist::add_back(int e) { v.push_back(e); len++; } void sqlist::delete_back() { v.pop_back(); len--; } void sqlist::insert_elem(int e,int i) { v.insert(v.begin()+ i,e); len++; } void sqlist::delete_elem(int i) { v.erase(v.begin()+ i); len--; } void sqlist::traverse() { for(int i=0;i<len;i++) cout<<"The element is "<< v.at(i)<<endl; ; } void sqlist::sort_sql() { sort(v.begin(),v.end()); }