C++ 的 STL -list 的教學小筆記

1. 放入資料
印出資料

#include <iostream>
#include <list>
using namespace std;

void PrintList(list<int> printList);	//印出方法

int main()
{
	list<int> myList;
	for (int i = 0; i < 5; i++)
		myList.push_back(i * 10);

	PrintList(myList);

	system("pause");
	return 0;
}
//印出方法
void PrintList(list<int> printList)
{
	list<int>::iterator it;
	for (it = printList.begin(); it != printList.end(); it++)
		cout << *it << endl;
}

2. 是否有資料

#include <iostream>
#include <list>
using namespace std;

bool IsContains(list<int> listData, int nElement);	//是否有這資料

int main()
{
	list<int> myList;
	for (int i = 0; i < 5; i++)
		myList.push_back(i * 10);

	cout << IsContains(myList, 20) << endl;
	cout << IsContains(myList, 5) << endl;
	
	system("pause");
	return 0;
}
//是否有這資料
bool IsContains(list<int> listData, int nElement)
{
	list<int>::iterator findIt = std::find(listData.begin(), listData.end(), nElement);
	if (findIt == listData.end())
		return false;
	else
		return true;
}

3. 取得資料 – 取得第幾個資料
取得第幾個資料
list 不像 vector 或陣列那樣可以直接用索引值存取
所以要另外用 iterator 去處理

#include <iostream>
#include <list>
using namespace std;

int GetListElementByIndex(list<int> listData, int nIndex);	//取得第幾個資料,從 0 開始

int main()
{
	list<int> myList;
	for (int i = 0; i < 5; i++)
		myList.push_back(i * 10);

	cout << GetListElementByIndex(myList, 2) << endl;	//取得第 2 個資料
	
	system("pause");
	return 0;
}

//取得第幾個資料,從 0 開始
int GetListElementByIndex(list<int> listData, int nIndex)
{
	if (nIndex < 0 || nIndex >= (int)listData.size())
	{
		return -1;		//error,回傳錯誤的方式,請視情況自行處理
	}

	list<int>::iterator it;
	it = listData.begin();
	for (int i = 0; i < nIndex; i++)
		it++;

	return *it;
}

4. 取得資料 it

#include <iostream>
#include <list>
using namespace std;

//如果要 return iterator 必須傳入指標
list<int>::iterator GetListItByElement(list<int>* pList, int nElement);	//用資料取得 it
list<int>::iterator GetListItByIndex(list<int>* pList, int nIndex);		//取得第幾個資料的 it

int main()
{
	list<int> myList;
	for (int i = 0; i < 5; i++)
		myList.push_back(i * 10);

	list<int>::iterator it;
	it = GetListItByElement(&myList, 10);	//取得數值資料 10 的 it
	cout << *it << endl;

	list<int>::iterator it2;
	it2 = GetListItByIndex(&myList, 2);		//取得第 2 個資料的 it
	cout << *it2 << endl;

	system("pause");
	return 0;
}
//用資料取得 it
list<int>::iterator GetListItByElement(list<int>* pList, int nElement)
{
	list<int>::iterator findIt = std::find(pList->begin(), pList->end(), nElement);
	return findIt;
}
//取得第幾個資料的 it
list<int>::iterator GetListItByIndex(list<int>* pList, int nIndex)
{
	if (nIndex < 0 || nIndex >= (int)pList->size())
		return pList->end();

	list<int>::iterator it;
	it = pList->begin();
	for (int i = 0; i < nIndex; i++)
		it++;

	return it;
}

5. 插入資料

#include <iostream>
#include <list>
using namespace std;

void PrintList(list<int> printList);
//如果要 return iterator 必須傳入指標
list<int>::iterator GetListItByElement(list<int>* pList, int nElement);	//用資料取得 it
list<int>::iterator GetListItByIndex(list<int>* pList, int nIndex);		//取得第幾個資料的 it

int main()
{
	list<int> myList;
	for (int i = 0; i < 5; i++)
		myList.push_back(i * 10);

	PrintList(myList);
	cout << endl;

	list<int>::iterator it;
	it = GetListItByIndex(&myList, 2);	//找到第 2 個資料的 it

	myList.insert(it, 15);	//插入數值 15

	PrintList(myList);

	system("pause");
	return 0;
}
//用資料取得 it
list<int>::iterator GetListItByElement(list<int>* pList, int nElement)
{
	list<int>::iterator findIt = std::find(pList->begin(), pList->end(), nElement);
	return findIt;
}
//取得第幾個資料的 it
list<int>::iterator GetListItByIndex(list<int>* pList, int nIndex)
{
	if (nIndex < 0 || nIndex >= (int)pList->size())
		return pList->end();

	list<int>::iterator it;
	it = pList->begin();
	for (int i = 0; i < nIndex; i++)
		it++;

	return it;
}
void PrintList(list<int> printList)
{
	list<int>::iterator it;
	for (it = printList.begin(); it != printList.end(); it++)
		cout << *it << endl;
}

6. 刪除資料
有兩種方法

#include <iostream>
#include <list>
using namespace std;

void PrintList(list<int> printList);
//如果要 return iterator 必須傳入指標
list<int>::iterator GetListItByElement(list<int>* pList, int nElement);	//用資料取得 it
list<int>::iterator GetListItByIndex(list<int>* pList, int nIndex);		//取得第幾個資料的 it

int main()
{
	list<int> myList;
	for (int i = 0; i < 5; i++)
		myList.push_back(i * 10);

	PrintList(myList);
	cout << endl;

	list<int>::iterator it;
	it = GetListItByIndex(&myList, 2);	//取得第 2 個資料的 it

	myList.erase(it);	//用 it 刪除

	PrintList(myList);
	cout << endl;

	myList.remove(30);	//刪除數值資料 30

	PrintList(myList);

	system("pause");
	return 0;
}
//用資料取得 it
list<int>::iterator GetListItByElement(list<int>* pList, int nElement)
{
	list<int>::iterator findIt = std::find(pList->begin(), pList->end(), nElement);
	return findIt;
}
//取得第幾個資料的 it
list<int>::iterator GetListItByIndex(list<int>* pList, int nIndex)
{
	if (nIndex < 0 || nIndex >= (int)pList->size())
		return pList->end();

	list<int>::iterator it;
	it = pList->begin();
	for (int i = 0; i < nIndex; i++)
		it++;

	return it;
}
void PrintList(list<int> printList)
{
	list<int>::iterator it;
	for (it = printList.begin(); it != printList.end(); it++)
		cout << *it << endl;
}

C++ 的 STL-在迴圈中刪除 iterator
https://husking-studio.com/cpp-iterator-erase-in-loop/

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *