小编Kyl*_*yle的帖子

使用带有c ++中的set的begin()和end()

我正在尝试使用迭代器来完成一个集合,然后对该集合的成员做一些事情(如果有的话).问题在于,通常这是有效的,但有时,它会比较空集的开头和结尾,并发现它们不相等.

感兴趣的代码段是:

    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++ set stdvector stdset

1
推荐指数
1
解决办法
1193
查看次数

函数返回期间出现奇怪的分段错误

我在两台不同的机器上运行一个程序。一方面它工作正常,没有问题。另一方面,它会导致分段错误。通过调试,我已经找到了故障发生的地方,但我想不出它发生的逻辑原因。

在一个函数中,我有以下代码:

pass_particles(particle_grid, particle_properties, input_data, coll_eros_track, collision_number_part, world, grid_rank_lookup, grid_locations);
cout<<"done passing particles"<<endl;
Run Code Online (Sandbox Code Playgroud)

函数 pass_particles 看起来像:

void pass_particles(map<int,map<int,Particle> > & particle_grid, std::vector<Particle_props> & particle_properties, User_input& input_data, data_tracking & coll_eros_track, vector<int> & collision_number_part, mpi::communicator & world, std::map<int,int> & grid_rank_lookup, map<int,std::vector<double> > & grid_locations)
{
     //cout<<"east-west"<<endl;
    //east-west exchange (x direction)
    map<int, vector<Particle> > particles_to_be_sent_east;
    map<int, vector<Particle> > particles_to_be_sent_west;
    vector<Particle> particles_received_east;
    vector<Particle> particles_received_west;
    int counter_x_sent=0;
    int counter_x_received=0;
    for(grid_iter=particle_grid.begin();grid_iter!=particle_grid.end();grid_iter++)
    {
        map<int,Particle>::iterator part_iter;
        for (part_iter=grid_iter->second.begin();part_iter!=grid_iter->second.end();)
        {
            if (particle_properties[part_iter->second.global_part_num()].particle_in_box()[grid_iter->first])
            {
                //decide if a particle has left …
Run Code Online (Sandbox Code Playgroud)

c++ segmentation-fault

1
推荐指数
1
解决办法
5166
查看次数

标签 统计

c++ ×2

segmentation-fault ×1

set ×1

stdset ×1

stdvector ×1