cpp-algo-cases/chapter_array_and_linkedlist/list.cpp

73 lines
1.6 KiB
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: list.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* Driver Code */
int main() {
/* <20><>ʼ<EFBFBD><CABC><EFBFBD>б<EFBFBD> */
vector<int> nums = {1, 3, 2, 5, 4};
cout << "<EFBFBD>б<EFBFBD> nums = ";
printVector(nums);
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
int num = nums[1];
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3>õ<EFBFBD> num = " << num << endl;
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
nums[1] = 0;
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1 <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>Ϊ 0 <20><><EFBFBD>õ<EFBFBD> nums = ";
printVector(nums);
/* <20><><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD> */
nums.clear();
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD> nums = ";
printVector(nums);
/* <20><>β<EFBFBD><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
nums.push_back(1);
nums.push_back(3);
nums.push_back(2);
nums.push_back(5);
nums.push_back(4);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>غ<EFBFBD> nums = ";
printVector(nums);
/* <20><><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
nums.insert(nums.begin() + 3, 6);
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 6 <20><><EFBFBD>õ<EFBFBD> nums = ";
printVector(nums);
/* ɾ<><C9BE>Ԫ<EFBFBD><D4AA> */
nums.erase(nums.begin() + 3);
cout << "ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3>õ<EFBFBD> nums = ";
printVector(nums);
/* ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD> */
int count = 0;
for (int i = 0; i < nums.size(); i++) {
count += nums[i];
}
/* ֱ<>ӱ<EFBFBD><D3B1><EFBFBD><EFBFBD>б<EFBFBD>Ԫ<EFBFBD><D4AA> */
count = 0;
for (int x : nums) {
count += x;
}
/* ƴ<><C6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD> */
vector<int> nums1 = {6, 8, 7, 10, 9};
nums.insert(nums.end(), nums1.begin(), nums1.end());
cout << "<EFBFBD><EFBFBD><EFBFBD>б<EFBFBD> nums1 ƴ<>ӵ<EFBFBD> nums ֮<>󣬵õ<F3A3ACB5> nums = ";
printVector(nums);
/* <20><><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD> */
sort(nums.begin(), nums.end());
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD> nums = ";
printVector(nums);
return 0;
}