unordered_set<int> st创建后内存里面是什么
时间: 2023-05-29 15:05:42 浏览: 54
unordered_set<int> st创建后,内存里面会有一个空的哈希表,用于存储元素。这个哈希表会根据需要自动扩容,并根据哈希函数将元素分配到不同的桶中。每个桶中可以存储多个元素,且元素是无序的。当插入元素时,会先根据哈希值找到对应的桶,然后在桶中查找是否已经存在该元素,如果不存在则将其插入到桶中。当查找元素时,也是根据哈希值找到对应的桶,然后在桶中查找是否存在该元素。因为unordered_set是无序的,所以不保证元素的插入和查找顺序。
相关问题
优化这份代码#include<iostream> #include<unordered_set> using namespace std; string a,b; int n; unordered_set<string> st; inline bool check(int l,int r){ for(register int i=0;i<n;i++){ if(a[i]==b[l]){ l++; } if(l>r){ return true; } } return false; } inline string strc(int l,int r){ string s=""; for(register int i=l;i<=r;i++){ s+=b[i]; } return s; } signed main(){ cin.sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>a>>b; bool flag=1; for(int i=0;i+1<n;i++){ if(b[i]!=b[i+1]){ flag=0; } } if(flag){ cout<<n; return 0; } int sum=0; for(register int i=0;i<n;i++){ for(register int j=i;j<n;j++){ string s=strc(i,j); if(check(i,j)&&!st.count(strc(i,j))){ sum++; st.insert(s); } } } cout<<sum; return 0; }
这份代码可以进行一些优化,以提高效率和减少不必要的计算。
首先,可以将函数 `strc` 的内部实现改为直接使用 `substr` 函数,而不是逐个字符拼接字符串。这样可以避免每次循环都重新创建字符串对象。更改后的函数实现如下:
```cpp
inline string strc(int l, int r) {
return b.substr(l, r - l + 1);
}
```
其次,可以将 `check` 函数中的循环改为从 `l` 开始遍历,而不是每次从 0 开始。因为在前面的循环中,已经保证了 `b[i]` 和 `b[i+1]` 相等的情况下,只有 `a[0]` 和 `b[i]` 匹配成功时才会继续匹配 `a` 的后续字符。因此,可以直接从 `l` 开始匹配。更改后的函数实现如下:
```cpp
inline bool check(int l, int r) {
for(register int i = l; i <= r; i++) {
if(a[i - l] != b[i]) {
return false;
}
}
return true;
}
```
最后,在内层循环中,可以添加一个判断条件,如果子串已经在集合 `st` 中,则无需进行后续的匹配和插入操作。修改后的代码如下:
```cpp
#include<iostream>
#include<unordered_set>
using namespace std;
string a, b;
int n;
unordered_set<string> st;
inline bool check(int l, int r) {
for(register int i = l; i <= r; i++) {
if(a[i - l] != b[i]) {
return false;
}
}
return true;
}
inline string strc(int l, int r) {
return b.substr(l, r - l + 1);
}
signed main() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> a >> b;
bool flag = true;
for(int i = 0; i + 1 < n; i++) {
if(b[i] != b[i + 1]) {
flag = false;
}
}
if(flag) {
cout << n;
return 0;
}
int sum = 0;
for(register int i = 0; i < n; i++) {
for(register int j = i; j < n; j++) {
string s = strc(i, j);
if(check(i, j) && !st.count(s)) {
sum++;
st.insert(s);
}
}
}
cout << sum;
return 0;
}
```
通过上述优化,可以提高代码的效率和性能。希望对你有帮助!如果还有其他问题,请随时提问。
用c++语言写一个程序完成图的深度优先遍历算法和广度优先遍历算法
好的,以下是深度优先遍历算法和广度优先遍历算法的示例代码(使用C++语言实现):
```c++
#include<iostream>
#include<queue>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using namespace std;
// 图的表示方式:邻接表
unordered_map<int, unordered_set<int>> graph;
// 深度优先遍历算法
void dfs(int start, unordered_set<int>& visited) {
stack<int> st; // 用栈来实现深度优先遍历
st.push(start);
while (!st.empty()) {
int node = st.top();
st.pop();
if (visited.find(node) == visited.end()) {
visited.insert(node);
for (const int& neighbor : graph[node]) {
st.push(neighbor);
}
}
}
}
// 广度优先遍历算法
void bfs(int start, unordered_set<int>& visited) {
queue<int> q; // 用队列来实现广度优先遍历
q.push(start);
while (!q.empty()) {
int node = q.front();
q.pop();
if (visited.find(node) == visited.end()) {
visited.insert(node);
for (const int& neighbor : graph[node]) {
q.push(neighbor);
}
}
}
}
int main() {
// 构建一个简单的图
graph[1] = {2, 3};
graph[2] = {1, 4, 5};
graph[3] = {1, 6};
graph[4] = {2};
graph[5] = {2, 6};
graph[6] = {3, 5};
unordered_set<int> visited;
// 深度优先遍历
dfs(1, visited);
cout<<"深度优先遍历结果:";
for (const int& node : visited) {
cout<<node<<" ";
}
cout<<endl;
visited.clear(); // 清空visited
// 广度优先遍历
bfs(1, visited);
cout<<"广度优先遍历结果:";
for (const int& node : visited) {
cout<<node<<" ";
}
cout<<endl;
return 0;
}
```
该示例代码中,我们使用了邻接表来表示图,深度优先遍历算法使用栈实现,广度优先遍历算法使用队列实现。在测试代码中,我们构建了一个简单的图,并对其进行了深度优先遍历和广度优先遍历,输出了遍历结果。