在cppreference上,我读到了以下内容std::vector<T,Allocator>::operator[]:
通过此运算符访问不存在的元素是未定义的行为。
标准草案中是否有类似的句子?或者说,我应该从标准的哪一部分推断出这个东西?
我想如果标准对此只字未提,那就会成为 UB。但我还没有找到任何相关信息std::vector<T,Allocator>::at,所以...
c++ std undefined-behavior language-lawyer indexoutofboundsexception
我想将自定义值插入到我的地图中,这发生在代码的第一部分中,但没有发生在第二个片段中。我的代码如下:
// First part of the code
unordered_map<int, int> hashmap;
for(int i=0; i<arr.size(); i++)
{
if(hashmap.find(arr[i]) != hashmap.end())
hashmap[arr[i]] += 1;
hashmap.insert(make_pair(arr[i], 1));
}
// Second part of the code
unordered_map<int, bool> hash2;
for(auto it=hashmap.begin(); it != hashmap.end(); ++it)
{
if(hash2[it->second] == true)
return false;
hash2.insert(make_pair(it->second, true));
}
return true;
Run Code Online (Sandbox Code Playgroud)
当我尝试创建自定义映射(键值对)并将“值”设置为 1 时,它正在工作,但是当我尝试在代码的第二部分中创建自定义映射时,它默认为false.
谁能帮我调试这个问题?
断言失败并且具有未定义的行为
#include<bits/stdc++.h>
const int MaxN = 1e5 + 10;
struct Node{
long long sum;
int left,right;
Node() :sum(0),left(0),right(0){
}
Node(long long sum,int left,int right) :sum(sum),left(left),right(right){
}
};
struct PersistentSegmentTree{
std::vector<Node> st;
std::vector<int> ver;
int n;
PersistentSegmentTree(int n) :n(n){
ver.push_back(0);
st.push_back(Node());
}
int update(int l,int r,int pos,int val,int oldId){
std::cerr << l << " " << r << "\n";
st.push_back(Node());
if(l == r){
st.back() = Node(val,0,0);
assert((int)st.size() - 1 != 0);
return (int)st.size() - 1;
}
int cur = (int)st.size() - …Run Code Online (Sandbox Code Playgroud) 当 C++ 创建std::ofstream它时,它立即并隐式创建底层文件。
我完全同意这种行为,除非我有一个代码,只有在运行期间才能看到是否会产生任何数据。
因此,我想避免在没有数据发送给空文件时创建空文件(事务完整性:没有数据,文件系统上没有更改)。
我看到两种我不太喜欢的方法:
tellg()),如果流为空,则删除该文件。我不喜欢创建和删除文件(有时文件很多)并且remove操作本身承担了太多责任。std::stringstream,收集输出并std::ofstream仅在字符串流不为空的情况下创建和复制内容。好多了,但仍然需要临时内存分配,这可能很大。对此有更好的解决方案吗?我是否缺少一些想法?
以代码的形式:
#include <fstream>
int main()
{
std::ofstream ofs("file.txt");
// Some code that might or might not output to ofs
// Some other code that might or might not output to ofs
// Some more code that might or might not output to ofs
// It would be nice if file is not created if no data sent to ofs
} …Run Code Online (Sandbox Code Playgroud) 为什么std :: vector的随机删除比std :: list更快?我正在做的是加快速度,将随机元素与最后一个交换,然后删除最后一个元素.我原以为列表会更快,因为随机删除就是为它构建的.
for(int i = 500; i < 600; i++){
swap(vector1[i], vector1[vector1.size()-1]);
vector1.pop_back();
}
for(int i = 0; i < 100; i++){
list1.pop_front();
}
Run Code Online (Sandbox Code Playgroud)
结果(以秒为单位):
Vec swap delete:0.00000909461232367903
列表正常删除:0.00011785102105932310
我正在使用eclipse 3.7.2并使用MinGW gcc 4.6.1作为我的编译器.每件事情都可以正常工作,并且我还在头文件中包含了我的源文件,但我无法在源文件中定义任何矢量类型.当我在下面的图像中注释出错误行时,每件事情都没问题并编译得很好.我无法弄清楚问题所在.
我有这样的代码:
typedef intptr_t ptr_t;
const int num_elements = 100;
ptr_t *pstr = (ptr_t *)malloc(sizeof(ptr_t) * num_elements);
std::array<ptr_t,num_elements> *parray = new (pstr) std::array<ptr_t,num_elements>;
Run Code Online (Sandbox Code Playgroud)
我希望能够将元素1洗牌到num_elements-2,所以我想使用std :: shuffle.
auto s = parray->begin()++;
auto e = parray->end()--;
std::random_shuffle ( s, e );
Run Code Online (Sandbox Code Playgroud)
我得到一个投诉,没有重载功能.我觉得自己无法看到自己做错了什么,我感到非常愚蠢.我该怎么做?
编辑:由于答案和反馈,它已改为
auto s = parray->begin();
s++;
auto e = parray->end();
std::random_shuffle ( s, e );
Run Code Online (Sandbox Code Playgroud)
但是,在'auto e'上我得到:'auto'的间接级别与'int*'不同
我能理解为什么我们应该在代码中使用这一行.这样,您就不必编写std :: cout或std :: cin等.
对于std :: string,如果我包含在c ++代码中,编译器是否会感到困惑?对于下面的变量str,它被认为是cstring类型的字符串还是std :: string?
include <cstring>
include <string>
using namespace std;
string str;
Run Code Online (Sandbox Code Playgroud)
换句话说,如果我在代码的开头有"using namespace std",那么将所有"std :: string"简单地写为"string"是否安全?另外如果我有"using namespace std",我不需要"使用std :: string",对吗?
这是如何实现的:vector <vector <T> > a;
每个向量都包含一个底层数组,但要有一个数组需要一个常量,但vector(它是外向量的数据类型)具有可变大小.如果它是由指针实现的,那么c ++知道何时使用指针以及何时使用直接值.
我有一个名为atom的自定义数据类型.我想用std :: transform来填充双向量,原子成员"number"是一个双倍的女巫.我得到错误"std :: vector :: iterator'没有名为'vec2'的成员",其中vec2是我的双向量.为什么是这样?甚至可以在变换中使用两种不同的数据类型吗?
atom.h
#ifndef _atom_
#define _atom_
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class atom{
public:
bool operator==(const atom rhs);
double number;
string name;
};
#endif
Run Code Online (Sandbox Code Playgroud)
atom.cpp
#include "atom.h"
atom::atom(){}
atom::~atom(){}
bool atom::operator==(const atom rhs){
return this->name==rhs.name;
}
Run Code Online (Sandbox Code Playgroud)
transformation.h
#ifndef _transformation_
#define _transformation_
#include "atom.h"
#include <vector>
#include <algorithm>
using namespace std;
struct transformation{
double operator() (atom a) const{
return a.number;
}
};
#endif
Run Code Online (Sandbox Code Playgroud)
main.cpp中
int main(){
vector<atom> vec;
atom …Run Code Online (Sandbox Code Playgroud)