cpp-algo-cases/chapter_array_and_linkedlist/list.cpp

77 lines
1.6 KiB
C++
Raw Normal View History

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