C++ list代码练习、set基础概念、set对象创建、set大小操作

对应力扣,回文链表,代码见下

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        list<int> li;
        ListNode *tmp = head;
        while(tmp){
            li.push_back(tmp->val);
            tmp = tmp->next;
        }
        for(list<int>::reverse_iterator it = li.rbegin(); it != li.rend(); it++){
            if((*it)!= head->val){
                return false;
            }
            head = head -> next;
        }
        return true;
    }
};

有序集合set的特点:容器内的元素不重复、每插入一个元素,容器内的元素都会进行有序排列。

还有另外一种mutiset:容器内的元素可以重复、每插入一个元素,容器内的元素都会进行有序排序。

容器特点:线性容器(vector、string、list),树形容器(set、multiset)

set对象创建,代码见下

cpp 复制代码
#include<iostream>
#include<set>

using namespace std;

void printSet(const set<int>& s) {
	for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {
	// 1 默认构造函数
	set<int> s1;
	cout << "s1: ";
	printSet(s1);

	// 2 初始化列表
	set<int> s2_1 = { 5, 4, 9, 3, 2, 1 };
	cout << "s2_1: ";
	printSet(s2_1);

	set<int> s2_2 = { 9, 8, 7, 7, 6, 5 };
	cout << "s2_2: ";
	printSet(s2_2);

	// 3 迭代器方式
	set<int> s3 = {s2_1.begin(), s2_1.end()};
	cout << "s3: ";
	printSet(s3);

	// 4 拷贝构造
	set<int> s4(s3);
	cout << "s4: ";
	printSet(s4);
	return 0;
}

set赋值操作,代码见下

cpp 复制代码
#include<iostream>
#include<set>

using namespace std;

void printSet(const set<int>& s) {
	for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {
	set<int> s = { 9, 8, 4, 3, 1, 2 };
	cout << "s: ";
	printSet(s);

	// 1 = set对象
	set<int> s1;
	s1 = s;
	cout << "s1: ";
	printSet(s1);

	// 2 = 初始化列表
	set<int> s2;
	s2 = { 3, 5, 4 };
	cout << "s2: ";
	printSet(s2);

	return 0;
}

list大小操作,代码见下

cpp 复制代码
#include<iostream>
#include<set>

using namespace std;

void printSet(const set<int>& s) {
	for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main() {
	set<int> s = { 9, 8, 4, 3, 1, 2 };
	cout << "s.enpty(): " << s.empty() << endl;
	cout << "s.size(): " << s.size() << endl;

	return 0;
}
相关推荐
苦夏木禾25 分钟前
js请求避免缓存的三种方式
开发语言·javascript·缓存
超级土豆粉33 分钟前
Turndown.js: 优雅地将 HTML 转换为 Markdown
开发语言·javascript·html
wei_shuo2 小时前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai
熊猫钓鱼>_>2 小时前
用Python解锁图像处理之力:从基础到智能应用的深度探索
开发语言·图像处理·python
小小小小王王王2 小时前
求猪肉价格最大值
数据结构·c++·算法
GO兔2 小时前
开篇:GORM入门——Go语言的ORM王者
开发语言·后端·golang·go
好开心啊没烦恼2 小时前
Python 数据分析:numpy,抽提,整数数组索引与基本索引扩展(元组传参)。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy·pandas
岁忧2 小时前
(LeetCode 面试经典 150 题 ) 58. 最后一个单词的长度 (字符串)
java·c++·算法·leetcode·面试·go
future14123 小时前
C#学习日记
开发语言·学习·c#
码农编程录3 小时前
【c/c++3】类和对象,vector容器,类继承和多态,systemd,std&boost
c++