1 链表实现
c++实现一个链表
#include <iostream>
#include <ostream>
using namespace std;
class myList{
/*
class相当于struct
struct功能上弱一点,stuct默认都是public
*/
struct Node{
Node(int x, Node *ptr=NULL):data(x), next(ptr) { } //在C++中struct可以直接初始化赋初值,这样只需传入data,ptr默认指向NULL
int data;
Node *next;
};
public:
myList():head(NULL) { }
~myList() { //析构函数释放节点内存空间
while(head)
{
Node *tem = head;
head = head->next;
delete tem;
}
}
void insert_head(int data)
{
Node *node = new Node(data);
node->next = head;
head = node;
}
friend ostream &operator<<(ostream &out, const myList &list); //通过友元类和运算符重定向,来实现<<符号打印链表,这里&代表引用
private:
Node *head;
};
ostream &operator<<(ostream &out, const myList &list) //重定向
{
myList::Node *tem = list.head;
while(tem)
{
out<< tem->data <<',';
tem = tem->next;
}
out << endl;
return out;
}
int main()
{
myList list;
list.insert_head(1);
list.insert_head(2);
list.insert_head(4);
list.insert_head(3);
cout << list;
}
2 迭代器和迭代器思想
目标是提供一种统一的方式来访问容器(如数组、列表、集合等)中的元素,而不暴露容器的内部表示。迭代器模式可以将遍历和容器分离,使得遍历算法独立于容器类型,从而使得容器和遍历算法可以相互独立地变化而不影响对方。
实现两个功能
- 指针可以++
- 可以直接取内容
2.1 以迭代器思想实现链表
#include <iostream>
#include <ostream>
using namespace std;
class myList{
struct Node{
Node(int x, Node *ptr=NULL):data(x), next(ptr) { }
int data;
Node *next;
};
public:
class iterator{
public:
iterator(Node *ptr=NULL):pos(ptr) { }
iterator &operator++(int)
{
if(NULL != pos)
pos = pos->next;
return *this;
}
int &operator*()
{
return pos->data;
}
bool operator!=(iterator x)
{
return pos != x.pos;
}
private:
Node *pos;
};
public:
myList():head(NULL) { }
~myList() {
while(head)
{
Node *tem = head;
head = head->next;
delete tem;
}
}
void insert_head(int data)
{
Node *node = new Node(data);
node->next = head;
head = node;
}
iterator begin()
{
return iterator(head);
}
iterator end()
{
return iterator(NULL);
}
friend ostream &operator<<(ostream &out, const myList &list);
private:
Node *head;
};
ostream &operator<<(ostream &out, const myList &list)
{
myList::Node *tem = list.head;
while(tem)
{
out<< tem->data <<',';
tem = tem->next;
}
out << endl;
return out;
}
int main()
{
myList list;
list.insert_head(1);
list.insert_head(2);
list.insert_head(4);
list.insert_head(3);
cout << list;
myList::iterator i = list.begin();
while(i != list.end() )
{
cout << *i <<endl;
i++;
}
}
2.2 高级用法使用模板
#include <iostream>
#include <ostream>
using namespace std;
template <typename T>
class myList{
struct Node{
Node(T x, Node *ptr=NULL):data(x), next(ptr) { }
T data;
Node *next;
};
public:
class iterator{
public:
iterator(Node *ptr=NULL):pos(ptr) { }
iterator &operator++(int)
{
if(NULL != pos)
pos = pos->next;
return *this;
}
int &operator*()
{
return pos->data;
}
bool operator!=(iterator x)
{
return pos != x.pos;
}
private:
Node *pos;
};
public:
myList():head(NULL) { }
~myList() {
while(head)
{
Node *tem = head;
head = head->next;
delete tem;
}
}
void insert_head(T data)
{
Node *node = new Node(data);
node->next = head;
head = node;
}
iterator begin()
{
return iterator(head);
}
iterator end()
{
return iterator(NULL);
}
template <typename X>
friend ostream &operator<<(ostream &out, const myList<X> &list);
private:
Node *head;
};
template <typename X>
ostream &operator<<(ostream &out, const myList<X> &list)
{
typename myList<X>::Node *tem = list.head;
while(tem)
{
out<< tem->data <<',';
tem = tem->next;
}
out << endl;
return out;
}
int main()
{
myList<int> list;
list.insert_head(1);
list.insert_head(2);
list.insert_head(4);
list.insert_head(3);
cout << list;
myList<int>::iterator i = list.begin();
while(i != list.end() )
{
cout << *i <<endl;
i++;
}
}
3 STL容器
STL容器提供了多种不同类型的数据结构,如向量(vector)、链表(list)、映射(map)、集合(set)等,每种容器都有其特定的功能和适用场景。通过使用STL容器,开发人员可以更轻松地管理数据,提高代码的可读性和可维护性。
- 顺序容器: vector list deque
- 适配器 statck queue priority_queue(基于上面容器实现的叫适配器)
- 关联容器: map set //tree multimap multiet
vector: 顺序表 insert(); push_back(); erase(); pop_back(); empty(); begin(); end(); …….
list: 链表 insert(); push_back(); erase(); pop_back(); empty(); front(); back(); sort();
deque: 双端 insert(); push_back(); erase(); pop_back(); empty(); Push_front();
stack: 适配器,它可以将任意类型的序列容器转换为一个堆栈,一般使用deque作为支持的序列容器。 元素只能后进先出(LIFO) push(); top(); pop();注意,出栈操作只是删除栈顶元素,并不返回该元素
queue: 适配器,它可以将任意类型的序列容器转换为一个队列,一般使用deque作为支持的序列容器。 元素只能先进先出(FIFO) push(); front()/back(); pop();注意,出栈操作只是删除栈顶元素,并不返回该元素
map:关联容器键值对(key/value)容器 map<string, double> stu; insert( make_pair<string, double>(“john”,95.5) ); stu[“keiven”] = 80.0; cout<<“john : ”<<stu[“john”] <<endl; cout<<“keiven : ”<<stu[“keiven”] <<endl;
set: set<int> a; a.insert(1); a.insert(3); a.insert(5); if(a.end != a.find(3) ) cout<<“have 3”<<endl; if(a.end() != a.find(30) ) cout<<“have 30”<<endl;
vector_list示例
#include <iostream>
#include <vector>
#include <list>
using namespace std;
int main()
{
#if 0
vector<int> arr;
arr.push_back(1);
arr.push_back(2);
arr.push_back(3);
arr.push_back(4);
arr.push_back(5);
#endif
// vector<double> arr;
list<double> arr;
arr.push_back(1.2);
arr.push_back(1.2);
arr.push_back(1.2);
arr.push_back(1.2);
arr.push_back(1.2);
// vector<int>::iterator i = arr.begin();
// vector<double>::iterator i = arr.begin();
list<double>::iterator i = arr.begin();
while(i != arr.end() )
{
cout<< *i <<endl;
i++;
}
}
map 示例
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, string> user_passwd;
user_passwd.insert(user_passwd.begin(), pair<string, string>("aaa", "1111") );
user_passwd.insert(user_passwd.begin(), pair<string, string>("aaa4", "114411") );
user_passwd.insert(user_passwd.begin(), pair<string, string>("aaa2", "111331") );
user_passwd.insert(user_passwd.begin(), pair<string, string>("aaa3", "111441") );
map<string, string>::iterator i = user_passwd.begin();
while(i != user_passwd.end())
{
cout<< (*i).first<< ',' <<(*i).second <<endl;
i++;
}
cout<< user_passwd["aaa2"] << endl;
}
高级泛型算法
algorethm示例
#include <iostream>
#include<algorithm>
using namespace std;
bool cmp(int a, int b)
{
return a>b;
}
void show(int data)
{
cout<< data<< endl;
}
bool fcmp(int data)
{
return data == 34;
}
int main()
{
//vector<int> arr;
int arr[] = {1,1234,23,4,23,42,34,23,42,34,2,2,2,444,22};
int n = sizeof(arr)/sizeof(int);
int *p = find_if(arr, arr+n, fcmp);
if(p != arr+n)
cout<<"got it !\n";
cout <<"num of 34: "<< count_if(arr, arr+n, fcmp) << endl;
/*
for(int i = 0; i<n; i++)
cout <<arr[i]<<',';
cout<<endl;
*/
for_each(arr, arr+n, show);
sort(arr, arr+n);
// sort(arr, arr+n, cmp);
cout<<"xxxxxxxxxxxxxxxxxxx\n";
unique(arr, arr+n);
for_each(arr, arr+n, show);
/*
for(int i = 0; i<n; i++)
cout <<arr[i]<<',';
cout<<endl;
*/
}