解释c++中的 list.swap
时间: 2023-08-07 08:03:00 浏览: 130
在 C++ STL 中,`list` 是一个双向链表容器,它提供了 `swap` 方法用于交换两个列表的内容。具体来说,`list.swap` 方法接受一个参数,表示要与当前列表交换内容的另一个列表。它会将两个列表的内容进行交换,但不会改变它们的大小(即容量不变)。例如:
```c++
#include <iostream>
#include <list>
int main() {
std::list<int> list1 = {1, 2, 3};
std::list<int> list2 = {4, 5, 6};
list1.swap(list2); // 交换 list1 和 list2 的内容
std::cout << "list1: ";
for (auto x : list1) {
std::cout << x << " ";
}
std::cout << std::endl;
std::cout << "list2: ";
for (auto x : list2) {
std::cout << x << " ";
}
std::cout << std::endl;
return 0;
}
```
输出结果为:
```
list1: 4 5 6
list2: 1 2 3
```
需要注意的是,`list.swap` 方法的时间复杂度是 O(1),因为它只是交换了两个指针,而不需要对列表中的元素进行复制或移动。
相关问题
C++ list的Swap实现
C++ 中的 std::list 没有 Swap 函数,但是可以通过编写代码实现两个 std::list 之间的交换。下面是一个示例:
```
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> a = {1, 2, 3, 4};
list<int> b = {5, 6, 7, 8};
a.swap(b);
cout << "a: ";
for(auto& i : a)
cout << i << " ";
cout << endl;
cout << "b: ";
for(auto& i : b)
cout << i << " ";
cout << endl;
return 0;
}
```
输出结果:
```
a: 5 6 7 8
b: 1 2 3 4
```
可以看到,通过 std::list 的成员函数 swap,可以将两个链表进行互换。
#include<iostream> using namespace std; struct LinkedList { // todo: some variables here void initialize() { int n; scanf("%d", &n); int *a = new int[n]; for(int i = 0; i < n; i++) scanf("%d", &a[i]); // todo delete[] a; } void insert(int i, int x) { // todo } void erase(int i) { // todo } void swap() { // todo } void moveback(int i, int x) { // todo } int query(int i) { // todo } void printAll() { // todo } void ClearMemory() { // todo } }; int main() { LinkedList List; int m, op, i, x; List.initialize(); scanf("%d", &m); while(m--) { scanf("%d", &op); if(op == 1) { scanf("%d%d", &i, &x); List.insert(i, x); } else if(op == 2) { scanf("%d", &i); List.erase(i); } else if(op == 3) { List.swap(); } else if(op == 4) { scanf("%d%d",&i, &x); List.moveback(i, x); } else if(op == 5) { scanf("%d", &i); printf("%d\n", List.query(i)); } else if(op == 6) { List.printAll(); } } List.ClearMemory(); return 0; }
抱歉,上述代码并没有实现您的要求,只是一个模版代码。以下是根据您的要求修改后的代码实现单链表:
```c++
#include<iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class LinkedList {
private:
ListNode *head, *tail;
int size;
public:
LinkedList(): head(NULL), tail(NULL), size(0) {}
void initialize() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
ListNode *node = new ListNode(x);
if(head == NULL) {
head = tail = node;
}
else {
tail->next = node;
tail = node;
}
size++;
}
}
void insert(int i, int x) {
if(i < 0 || i > size) return;
ListNode *node = new ListNode(x);
if(i == 0) {
node->next = head;
head = node;
if(tail == NULL) tail = head;
}
else if(i == size) {
tail->next = node;
tail = node;
}
else {
ListNode *cur = head;
for(int j = 0; j < i - 1; j++) {
cur = cur->next;
}
node->next = cur->next;
cur->next = node;
}
size++;
}
void erase(int i) {
if(i < 0 || i >= size) return;
ListNode *cur = head, *pre = NULL;
for(int j = 0; j < i; j++) {
pre = cur;
cur = cur->next;
}
if(pre == NULL) {
head = head->next;
if(head == NULL) tail = NULL;
}
else {
pre->next = cur->next;
if(pre->next == NULL) tail = pre;
}
delete cur;
size--;
}
void swap() {
if(size <= 2) return;
ListNode *cur = head, *pre = NULL;
while(cur && cur->next) {
ListNode *tmp = cur;
cur = cur->next;
tmp->next = cur->next;
cur->next = tmp;
if(pre) pre->next = cur;
pre = tmp;
cur = tmp->next;
}
if(size % 2 == 0) tail = pre;
}
void moveback(int i, int x) {
if(i <= 0 || i > size) return;
ListNode *cur = head, *pre = NULL;
for(int j = 0; j < i - 1; j++) {
pre = cur;
cur = cur->next;
}
for(int j = 0; j < x; j++) {
if(cur == NULL) cur = head;
ListNode *tmp = cur->next;
if(pre == NULL) {
head = tmp;
tail->next = cur;
cur->next = NULL;
tail = cur;
}
else if(tmp == NULL) {
pre->next = NULL;
tail = pre;
cur->next = head;
head = cur;
}
else {
pre->next = tmp;
cur->next = tmp->next;
tmp->next = cur;
}
pre = cur;
cur = cur->next;
}
}
int query(int i) {
if(i < 1 || i > size) return -1;
ListNode *cur = head;
for(int j = 1; j < i; j++) {
cur = cur->next;
}
return cur->val;
}
void printAll() {
ListNode *cur = head;
while(cur) {
printf("%d ", cur->val);
cur = cur->next;
}
printf("\n");
}
void clearMemory() {
ListNode *cur = head;
while(cur) {
ListNode *tmp = cur;
cur = cur->next;
delete tmp;
}
head = tail = NULL;
size = 0;
}
};
int main() {
LinkedList list;
int m, op, i, x;
list.initialize();
scanf("%d", &m);
while(m--) {
scanf("%d", &op);
if(op == 1) {
scanf("%d%d", &i, &x);
list.insert(i, x);
}
else if(op == 2) {
scanf("%d", &i);
list.erase(i);
}
else if(op == 3) {
list.swap();
}
else if(op == 4) {
scanf("%d%d", &i, &x);
list.moveback(i, x);
}
else if(op == 5) {
scanf("%d", &i);
printf("%d\n", list.query(i));
}
else if(op == 6) {
list.printAll();
}
}
list.clearMemory();
return 0;
}
```
阅读全文