C 中存在一些代码,用于calloc()创建有效的向量。它看起来像这样:
uint64_t *reverseOrder = (uint64_t *)calloc((size + 1), sizeof(uint64_t));
Run Code Online (Sandbox Code Playgroud)
我想用 C++ 语法和向量来模仿这种行为,以便它实际上具有相同的效果。我可以使用以下语法吗?
std::vector<uint64_t> reverseOrder(size + 1, 0);
Run Code Online (Sandbox Code Playgroud)
我知道calloc()实际上会遍历内存并将它们设置为 0,所以我想知道是否是这种情况。
这是我的代码:
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
v.push_back(6);
v.push_back(7);
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
{
if (*it == 7)
v.erase(it);
cout << *it << endl;
}
Run Code Online (Sandbox Code Playgroud)
问题是,for循环不会停止运行并打印垃圾值。您能否对这个问题给出一些见解...比如最后一个元素可能存储有关大小或地址的信息end()?
使用该erase()方法对于向量中的其他位置(最后一个元素除外)效果很好。
控制台日志显示垃圾值,如下所示:
34603778
35652354
50397954
34603592
34603536
34603536
34603536
34603536
34603794
36700688
34603536
34603536
34865684
51511824
34603536
34865680
Run Code Online (Sandbox Code Playgroud) 首先我动态分配一个对象:
Object* someObject = new Object();
Run Code Online (Sandbox Code Playgroud)
然后我将该指针的地址插入到向量中:
std::vector<Object**> someVector0;
someVector0.push_back(&someObject);
Run Code Online (Sandbox Code Playgroud)
然后我将居住地址的地址插入someVector0到另一个向量中:
std::vector<Object***> someVector1;
someVector1.push_back(&someVector0[0]);
Run Code Online (Sandbox Code Playgroud)
最后,我将上述地址插入someVector1到另一个向量中:
std::vector<std::vector<Object***>*> someVector2;
someVector2.push_back(&someVector1);
Run Code Online (Sandbox Code Playgroud)
然后我使用获得的向量,现在我需要释放分配的内存:
//Do some stuff
//Free memory
Run Code Online (Sandbox Code Playgroud)
for (int i = 0; i < someVector2.size(); i++) {
for (int j = 0; j < (*someVector2[i]).size(); j++) {
delete *(*(*someVector2[i])[j]);
delete *(*someVector2[i])[j];
delete (*someVector2[i])[j];
}
delete someVector2[i];
}
Run Code Online (Sandbox Code Playgroud)
我曾怀疑这段代码足以解除分配,但是,我在解除分配时遇到了一些问题。我怀疑我可能已经取消引用了 anullptr但我想不出其他任何东西,我删除指针的方式可能有错误吗?
注意:这是我在较大规模的程序中遇到的问题的简化版本,前面提到的向量都不是不必要的,我不想处理任何副本,不要更改向量的类型。
编辑:
您好,在进行了更多研究并听取了评论之后,我已经醒悟了,尽管我认为没有一个人会认为我的代码以任何方式合理,但我强烈敦促任何可能认为这是一种有效方法的人做任何不做的事情。正如评论已经指出的那样,我对解决方案的尝试表明我不知道自己在做什么,所以请不要这样做;它实际上是无法维护的。相反,正如另一条评论已经指出的那样,请使用智能指针或真正了解代码中发生的情况。
我们可以对向量的向量进行排序吗:
{[7, 2], [1, 8], [3, 4]}
Run Code Online (Sandbox Code Playgroud)
使用std::sort(vec.begin(), vec.end()), 根据每个向量中的第一个元素按升序排列,或者我们是否需要将自定义比较器函数传递给std::sort?
有人可以告诉我我做错了什么吗?我正在尝试按降序访问元素。我无法修改输入数据,因此std::sort不是一个选项。
我尝试使用std::max_element,但是当它等于 时std::begin(),不知何故我找不到当前最大值的最小值。这是为什么?
const std::vector<int> v{2, 1, 4, 5, 0, 6, 4, 3, 2};
auto max = std::max_element(v.begin(), v.end());
auto min = std::min_element(v.begin(), v.end());
auto tmp = std::begin(v);
while(*max != *min)
{
tmp = std::max_element(v.begin(), v.end(),
[max](const auto& item1, const auto& item2)
{
return item2 < *max && item1 < item2;
});
max = tmp;
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有成员变量的类std::vector。我还有一个成员函数,它采用std::vector由两个size_t参数指定的连续范围,并对其执行一些操作。
class MyClass {
std::vector<int> objects;
public:
void func(size_t start_index, size_t end_index);
}
Run Code Online (Sandbox Code Playgroud)
现在,我想size_t用单个参数替换两个std::span参数,以提高安全性、可读性和可维护性。然而,有一个障碍:在我的函数中,我需要能够恢复原始std::vector. 就像这样,我需要能够找到std::span原始数组中的开始位置。有没有办法做到这一点?如果没有,那么我是否别无选择,只能使用我原来笨重的设计?
objects.begin()(我最初尝试通过从我拥有的参数begin()的迭代器中减去来恢复第一个索引std::span,但这导致了编译错误。)
我正在尝试使用迭代器来完成一个集合,然后对该集合的成员做一些事情(如果有的话).问题在于,通常这是有效的,但有时,它会比较空集的开头和结尾,并发现它们不相等.
感兴趣的代码段是:
for(int i=0;i<input_data.num_particles();i++)
{
//loop through pairs contained in particle i's Verlet list
set<int>::iterator iter;
for(iter=verlet_vars.verlet()[i].begin();iter!=verlet_vars.verlet()[i].end();iter++)
{
//call the force() function to calculate the force between the particles
force(particles.getpart(i),particles.getpart(*iter),input_data,*iter);
}
}
Run Code Online (Sandbox Code Playgroud)
有时,即使verlet_vars.verlet()[i]中包含的集合为空,程序也会将迭代器与集合的末尾进行比较并发现它们不相等,因此它进入内部循环(最终导致程序崩溃)通过尝试调用force()函数).奇怪的是,如果我在调用内部循环之前对迭代器做了什么,比如做类似的事情:
iter=verlet_vars.verlet()[i].begin();
Run Code Online (Sandbox Code Playgroud)
然后,内部循环的比较总是返回true,程序正常运行.
PS命令verlet_vars.verlet()[i]调用集合的向量,因此[i]
verlet()函数:
std::vector<std::set<int> > verlet() const {return _verlet;}
Run Code Online (Sandbox Code Playgroud)
谢谢你的时间.
我不是一个c ++编码器,所以可能很容易.
我有一个Point类的向量,我想找到AABB矩形:
我已经完成了一个for循环,保存了min和max(一次为x,一次为y),并用一些ifs更新每次迭代的值.
但我确信在std或boost中有更聪明的东西.
例如我刚试过:
vector<ofPoint> points;
// ....
bool _compareX(ofPoint const &p1, ofPoint const &p2) { return p1.x > p2.x; }
bool _compareY(ofPoint const &p1, ofPoint const &p2) { return p1.y > p2.y;}
void DrawerWidget::foo()
{
cout << std::min_element(points.begin(), points.end(), &_compareX) << endl;
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个奇怪的错误,如
错误:'std :: cout << std :: min_element中没有匹配'operator <<'[与_FIter = __gnu_cxx :: __ normal_iterator …
我有这样的Class粒子,
class Particle
{
public:
std::vector<Particle*> getNbh () const;
void setNbh (const std::vector<Particle*>&);
private:
std::vector<Particle*> nbh_;
};
Run Code Online (Sandbox Code Playgroud)
并且功能Particle::setNbh(const std::vector<Particle*>&)已实现,
void Particle::setNbh (const std::vector<Particle*>& nbh)
{
nbh_ = nbh;
}
Run Code Online (Sandbox Code Playgroud)
然后有一个非会员功能 updateNeighbors (std::vector<Particle>& particles, double cutoff)
void updateNeighbors (std::vector<Particle>& particles, double cutoff)
{
for (auto particle : particles)
{
auto nbh = std::vector<Particle*>();
for (auto other : particles)
if (&particle != &other
&& norm(particle.getPosition() - other.getPosition()) < cutoff)
nbh.push_back(&other);
particle.setNbh(nbh);
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我用这个函数更新邻居时,nbh_成员没有正确更新,我测试它打印getNbh()每个粒子的大小.
这是正确的复制方式,std::vector<Particle*> …
我有一个User类,私人用户可以个性化用户的个人资料,例如姓名,年龄,收藏夹.我想比较两个给定用户的收藏夹并输出常见的收藏夹.因为我将使用向量,我的问题是如何将用户分组到vector(user1,user2),这样我就可以比较任何给定成员的收藏并输出结果.这就是我到目前为止所拥有的
using namespace std;
vector<string> user;
int adduser();
int adduser()
{
ofstream thefile;
thefile.open("users.txt", ios::app);
string firstname;
string lastname;
int age;
string favourites;
cout<<"Enter your Name: ";
cin>>name;
thefile << name << ' ';
cout<<"Enter your age: ";
cin>>age;
thefile << age << ",";
cout<<"Enter your favourites: ";
cin>>favourites;
thefile << favourites<< ",";
thefile.close();
cout<<" Completed."<<endl;
return 0;
}
common favourites()
{
//how do make it so i have something like user1 and user2
//which are both vectors so …Run Code Online (Sandbox Code Playgroud)