getline(cin, a); // 读取整行输入
使用 getline(cin, a)
来读取一整行输入,这样可以包含空格并将整行作为字符串存储在变量 a
中
字符串
sort
#include <algorithm>
sort(result.begin(),result.end());
substr(i-8, 8)
在截取子字符串时,a.substr(i-8, i)
的第二个参数应该是截取的长度而不是结束位置。因此,应该将 a.substr(i-8, i)
改为 a.substr(i-8, 8)
。
#include <iostream>
using namespace std;
int main() {
string a, b;
int i = 8;
cin >> a;
int length = a.length();
while (length>=i) {
cout << a.substr(i-8, 8) << endl;
i += 8;
}
if ((length+8-i)>0 ) {
b = a.substr(i-8, length+8-i);
for (int j=0; j < i - length; j++) {
b += "0";
}
cout << b << endl;
}
}
字符串补充指定数量字符
str.append(count, '0');
进制转化
十进制转十六进制 pow(16,i)
#include <iostream>
#include <stack>
#include <cmath>
using namespace std;
int main() {
string a;
cin>>a;
int ans=0;
stack<char>stk;
for(int i=2;i<a.length();i++)
{
stk.push(a[i]);
}
for(int i=0;i<a.length()-2;i++)
{
if (!stk.empty()) {
if(stk.top()>='A'&&stk.top()<='F')ans+=((stk.top()-'A'+10)*pow(16,i));
if(stk.top()>='a'&&stk.top()<='f')ans+=((stk.top()-'a'+10)*pow(16,i));
if(stk.top()>='0'&&stk.top()<='9')ans+=((stk.top()-'0')*pow(16,i));
stk.pop();
}
}
cout<<ans;
}
map
map遍历
#include <map>
map<int, int> myMap = {{1, 10}, {2, 20}, {3, 30}};
// 使用迭代器遍历map
for (auto it = myMap.begin(); it != myMap.end(); ++it) {
cout << "Key: " << it->first << ", Value: " << it->second << endl;
}
map寻找键值
map.find(key)==map.end()
与unordered_map区别
#include<unordered_map>
元素的唯一性:
map
中的键是唯一的,如果插入已存在的键,则会更新对应的值。unordered_map
中的键也是唯一的,但是由于哈希冲突的存在,可能会有多个键映射到同一个哈希槽,这时会使用链表或其他方法解决冲突。
有序性:
map
:元素按照键的大小顺序存储,可以通过自定义比较函数来实现不同的排序方式。unordered_map
:元素在哈希表中无序存储,不保证元素的顺序,因此无法直接对unordered_map
进行排序。
cin与getline区别
cin:不会把空格后面的字符串读取进去。因此,它只能读取一个。
getline:输入的内容可以原模原样地输出。因此,它能读取一行。
滑动窗口
class Solution {
public:
int lengthOfLongestSubstring(string s) {
//哈希表记录元素最后出现的位置
unordered_map<char, int> hash;
int ans = 0, left = 0;
int i;
for(i = 0; i < s.length(); ++i){
char cur = s[i];
if(hash.count(cur)){
ans = max(ans, i - left);
left = max(hash[cur] + 1,left);//例如abba 防止跑回到第一个a位置
}
hash[cur] = i;
}
return max(ans, i - left);
}
};
二维数组
vector<string> mat(r, string(c, 0));
创建一个名为 mat
的二维字符串向量,其中包含 r
行和每行包含 c
个字符。每个字符初始化为 ASCII 值为 0 的字符。这样的初始化将产生一个具有指定行数和每行指定长度的二维字符串向量,其中每个字符都初始化为字符 '0'
,而不是数字 0。
INT_MIN 和 INT_MAX
INT_MIN 和 INT_MAX是表示整数的最小值和最大值,它们分别为-2147483648和2147483647。
链表
结构体
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 operateList {
public:
void createList(ListNode* head) {
ListNode* phead = head;
for (int i = 1; i < 10; i++) {
ListNode* node = new ListNode;
node->val = i;
node->next = NULL;
phead->next = node;
phead = node;
}
//cout << "创建成功\n";
}
void printList(ListNode* head)
{
ListNode* temp=head;
while(temp->next!=NULL)
{
cout<<temp->val<<" ";
temp=temp->next;
}
cout<<temp->val<<endl;
}
};
内存释放
创建链表后要释放内存
class Solution {
public:
int getLength(ListNode* head) {
int length = 0;
while (head) {
++length;
head = head->next;
}
return length;
}
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy = new ListNode(0, head);
int length = getLength(head);
ListNode* cur = dummy;
for (int i = 1; i < length - n + 1; ++i) {
cur = cur->next;
}
cur->next = cur->next->next;
ListNode* ans = dummy->next;
delete dummy;
return ans;
}
};
关键字
static constexpr int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
在C++中,静态成员变量需要在类外进行定义和初始化
// 在类外定义和初始化静态成员变量
constexpr int Solution::directions[4][2];
-
static
:- 在全局变量或函数中,
static
的作用是将其作用域限定在当前文件或当前函数中,不会被其他文件访问。 - 在类的成员函数中,
static
的作用是将成员变量或成员函数与类的实例对象解耦,可以通过类名::成员名的方式访问,而不需要创建类的实例对象。
- 在全局变量或函数中,
-
constexpr
:constexpr
是 C++11 引入的关键字,用于声明常量表达式。常量表达式是在编译时就可以计算出结果的表达式。- 在你的代码中,
constexpr
用于声明directions
数组为常量表达式,这意味着编译器可以在编译时计算出directions
数组的值,并在程序运行时直接使用这个计算出来的值,从而提高程序的性能和效率。 static constexpr int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
的作用是定义一个二维数组directions
,并将其声明为静态常量表达式,用于表示四个方向的移动偏移量。
二叉树
二叉树创建
// 层序遍历创建二叉树
TreeNode* createBinaryTree(vector<int>& values) {
if (values.empty()) {
return nullptr;
}
TreeNode* root = new TreeNode(values[0]);
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
for (int i = 1; i < values.size(); i += 2) {
TreeNode* currentNode = nodeQueue.front();
nodeQueue.pop();
if (values[i] != -1) { // -1 表示空节点
currentNode->left = new TreeNode(values[i]);
nodeQueue.push(currentNode->left);
}
if (i + 1 < values.size() && values[i + 1] != -1) {
currentNode->right = new TreeNode(values[i + 1]);
nodeQueue.push(currentNode->right);
}
}
return root;
}
二叉树输出
// 层序遍历打印二叉树(用于验证创建的二叉树是否正确)
void printBinaryTree(TreeNode* root) {
if (!root) {
cout << "Binary tree is empty." << endl;
return;
}
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty()) {
TreeNode* currentNode = nodeQueue.front();
nodeQueue.pop();
cout << currentNode->val << " ";
if (currentNode->left) {
nodeQueue.push(currentNode->left);
}
if (currentNode->right) {
nodeQueue.push(currentNode->right);
}
}
cout << endl;
}
二维数组
赋值方法一
vector<int> rowValues; // 用于存储每行的数值
for(int j=pow(2,i);j<ans.size()+1;j++)
{
if(ans[j-1]!=-1)
{
rowValues.push_back(ans[j-1]);
}
}
nums.push_back(rowValues); // 将当前行的数值加入到 nums 中
赋值方法二
ans.push_back(vector<int>{});//切换到下一行
for(int i=0;i<currentsize;i++)
{
TreeNode* CurrentNode=que.front();
ans.back().push_back(CurrentNode->val);//往当前行最后一个元素追加元素
}
输出
vector<vector<int>>ans=sol.levelOrder2(head);
for (const auto& row : ans) {
for (int val : row) {
cout << val << " ";
}
cout <<endl;
}
有序二维数值进行二分查找
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size(), n = matrix[0].size();
int low = 0, high = m * n - 1;//转成一维数组获得个数
while (low <= high) {
int mid = (high - low) / 2 + low;
int x = matrix[mid / n][mid % n];//转回二维数组 获得中间值
if (x < target) {
low = mid + 1;
} else if (x > target) {
high = mid - 1;
} else {
return true;
}
}
return false;
}
};