vector<int>实质
时间: 2024-06-02 19:03:46 浏览: 21
vector<int>是C++标准库中的一个容器类,用于存储一组整数类型的元素。它可以动态地调整大小,并且提供了一系列的成员函数来方便地对元素进行操作。
实质上,vector<int>是一个动态数组,它在内存中以连续的方式存储整数类型的元素。当我们向vector中添加元素时,如果当前容量不足以容纳新的元素,vector会自动重新分配更大的内存空间,并将原有的元素复制到新的内存空间中。这种动态调整大小的特性使得vector非常灵活和高效。
除了动态调整大小外,vector还提供了一系列的成员函数,例如push_back()用于在末尾添加元素、pop_back()用于删除末尾元素、size()用于获取当前元素个数等等。通过这些成员函数,我们可以方便地对vector进行插入、删除、访问等操作。
总结一下,vector<int>实质上是一个动态调整大小的数组,它提供了丰富的成员函数来方便地对整数类型的元素进行操作。
相关问题
代码#include <iostream> #include <unordered_set> #include <algorithm> #include <vector> using namespace std; vector<pair<int, int>> findStonePairs(vector<int> &weights, int d) { unordered_set<int> stoneSet; vector<pair<int, int>> pairs; for (int i = 0; i < weights.size(); i++) { int weight = weights[i]; if (stoneSet.find(weight - d) != stoneSet.end()) { auto j = find(weights.begin(), weights.end(), weight - d) - weights.begin(); pairs.push_back({weights[j], weights[i]}); } if (stoneSet.find(weight + d) != stoneSet.end()) { auto j = distance(weights.begin(), find(weights.begin(), weights.end(), weight + d)); pairs.push_back({weights[j], weights[i]}); } stoneSet.insert(weight); } return pairs; } int main() { int d = 2; vector<int> weights = {1, 3, 3, 5, 4, 2, 7, 1}; vector<pair<int, int>> pairs = findStonePairs(weights, d); for (auto p : pairs) { cout << "[" << p.first << ", " << p.second << "]" << endl; } // for (auto p : pairs) // { // cout << "[" << weights[p.first] << ", " << weights[p.second] << "]" << endl; // } return 0; }的时间复杂度
这段代码的时间复杂度为 O(n^2),其中 n 为 weights 数组的长度。因为代码中使用了一个循环遍历 weights 数组,再在内部使用了两次 find 或 distance 函数查找 stoneSet 中是否存在特定的元素,这两个函数本质上也是一个循环遍历,因此总的时间复杂度就是 O(n^2)。
void Forecast() { while (true) { string serial; serial.resize(100); printf("请输入序列:"); scanf("%s", &serial[0]); vector<string> stack; stack.push_back("$"); stack.push_back("E"); string strs(serial.c_str()); string w = strs + "$"; int
void Forecast() {
while (true) {
string serial;
serial.resize(100);
printf("请输入序列:");
scanf("%s", &serial[0]);
vector<string> stack;
stack.push_back("$");
stack.push_back("E");
string strs(serial.c_str());
string w = strs + "$";
int ip = 0;
string X = stack[stack.size() - 1];
TreeNode root = new Node();
root->name = X;
vector<Node*> tree_stack;
tree_stack.push_back(root);
tree_stack.push_back(root);
TreeNode parent = root;
TreeNode node;
string a = NextFlag(ip, w);
bool isSuc = true;
do {
if (IfStringIn(listTerminal, X)) {
if (X == a) {
tree_stack.pop_back();
parent = tree_stack[tree_stack.size() - 1];
stack.pop_back();
X = stack[stack.size() - 1];
a = NextFlag(ip, w);
} else {
printf("语法错误行:“%s”,错误位置列索引:“%d”", serial.c_str(), ip - 1);
isSuc = false;
break;
}
} else {
int nStart = 0;
if (NextFlag(nStart, M[X][a]) == X) {
tree_stack.pop_back();
stack.pop_back();
vector<string> stack2;
string strTemp = NextFlag(nStart, M[X][a]);
while (strTemp != "") {
if (strTemp != "ε") {
stack2.push_back(strTemp);
}
strTemp = NextFlag(nStart, M[X][a]);
}
while (stack2.size() > 0) {
node = new Node();
node->name = stack2[stack2.size() - 1];
tree_stack.push_back(node);
parent->child.push_back(node);
stack.push_back(stack2[stack2.size() - 1]);
stack2.pop_back();
}
parent = tree_stack[tree_stack.size() - 1];
X = stack[stack.size() - 1];
} else {
printf("语法错误行:“%s”,错误位置列索引:“%d”", serial.c_str(), ip - 1);
isSuc = false;
break;
}
}
} while (X != "$" || a != "$");
if (isSuc) {
PrintTree(root);
printf("\r\n语法分析成功\r\n\r\n");
} else {
printf("\r\n\r\n");
}
}
}
以上是经过格式化后的代码,没有进行实质性的修改。