list .insert() & rbegin()

本文介绍了使用C++进行列表操作的方法,包括插入元素、使用反向迭代器以及自定义结构体的排序技巧。通过具体代码示例展示了如何在列表中插入不同数量的元素,并对包含自定义结构体指针的列表进行排序。

#include <iostream>
#include <stdlib.h>
#include <list>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
  list<int> mylist;
  list<int>::iterator it;

  // set some initial values:
  for (int i=1; i<=5; i++) mylist.push_back(i); // 1 2 3 4 5

  it = mylist.begin();
  ++it;       // it points now to number 2           ^

  mylist.insert (it,10);                        // 1 10 2 3 4 5

  // "it" still points to number 2                      ^
  mylist.insert (it,2,20);                      // 1 10 20 20 2 3 4 5

  --it;       // it points now to the second 20            ^

  vector<int> myvector (2,30);
  mylist.insert (it,myvector.begin(),myvector.end());
                                                // 1 10 20 30 30 20 2 3 4 5
                                                //               ^
  cout << "mylist contains:";
  for (it=mylist.begin(); it!=mylist.end(); it++)
  cout << " " << *it;
  cout << endl;

   list<int> mlist;
   mlist.insert (mlist.begin(),mylist.rbegin(),mylist.rend());
                                                // 1 10 20 30 30 20 2 3 4 5
                                                //               ^
  cout << "mylist contains:";
  for (it=mlist.begin(); it!=mlist.end(); it++)
  cout << " " << *it;
  cout << endl;

  system("PAUSE"); 
  return 0;
}

改代码在DEV-C++中科顺利通过,但是在VC中编译时会出现errorC2664的错误,原因在于mlist.insert (mlist.begin(),mylist.rbegin(),mylist.rend());这一行,VC++中的rbegin与标准C++的还是有区别的,当然利用VC的反向iterator或者在标准范围内亦可解决问题。

 

 

 

 

 

 

 

 

 

 

 

 

code learning

默认分类    2008-08-28 22:19   阅读5   评论0  
字号:    

#include <iostream>
#include <stdlib.h>
#include <list>  
#include <iterator>  
#include <algorithm>  
#include <string>  
using   namespace   std;  

struct   Person{  
    string   name;
    string   add;
    Person(const string &_name,const string &_add):name(_name),add(_add){}
    };  
class   My_less{
    public:
        inline
        bool operator()(const Person *p1,const Person *p2)const{
            return   p1->name<p2->name;
            }
        };  

int main(int argc, char *argv[]){
    Person   *p1=new   Person("ddd","shanghai");
    Person   *p2=new   Person("bbb","nanjing");
    Person   *p3=new   Person("ccc","wuhan");  
           
    list<Person*>   coll;  
    list<Person*>::iterator   it;  
           
    coll.push_back(p1);  
    coll.push_back(p2);  
    coll.push_back(p3);  
           
    for(it=coll.begin();it!=coll.end();++it){
        cout<<(*it)->name<<"   "<<(*it)->add<<endl;
        }  
    coll.sort(My_less());  
    cout<<"-------"<<endl;  
    for(it=coll.begin();it!=coll.end();++it){
        cout<<(*it)->name<<"   "<<(*it)->add<<endl;
        }  
    for(int u =0; u <5; ++u){
        int x = u;
        cout<< " u = "<<u<< " x = "<<x<<endl;
        }
  system("PAUSE"); 
  return 0;
}

#include &lt;queue&gt; #include &lt;vector&gt; #include &lt;unordered_set&gt; #include &lt;costmap_2d/cost_values.h&gt; #include &quot;common/structure/node.h&quot; #include &quot;path_planner/graph_planner/jastar_planner.h&quot; namespace rmp { namespace path_planner { /** * @brief Construct a new AStar object * @param costmap the environment for path planning * @param dijkstra using diksktra implementation * @param gbfs using gbfs implementation */ J_AStarPathPlanner::J_AStarPathPlanner(costmap_2d::Costmap2DROS* costmap_ros, double obstacle_factor, bool dijkstra, bool gbfs) : PathPlanner(costmap_ros, obstacle_factor) { // can not using both dijkstra and GBFS at the same time if (!(dijkstra &amp;&amp; gbfs)) { is_dijkstra_ = dijkstra; is_gbfs_ = gbfs; } else { is_dijkstra_ = false; is_gbfs_ = false; } }; /** * @brief A* implementation * @param start start node * @param goal goal node * @param path optimal path consists of Node * @param expand containing the node been search during the process * @return true if path found, else false */ bool J_AStarPathPlanner::plan(const Point3d&amp; start, const Point3d&amp; goal, Points3d&amp; path, Points3d&amp; expand) { double m_start_x, m_start_y, m_goal_x, m_goal_y; if ((!validityCheck(start.x(), start.y(), m_start_x, m_start_y)) || (!validityCheck(goal.x(), goal.y(), m_goal_x, m_goal_y))) { return false; } Node start_node(m_start_x, m_start_y); Node goal_node(m_goal_x, m_goal_y); start_node.set_id(grid2Index(start_node.x(), start_node.y())); goal_node.set_id(grid2Index(goal_node.x(), goal_node.y())); // clear vector path.clear(); expand.clear(); // open list and closed list std::priority_queue&lt;Node, std::vector&lt;Node&gt;, Node::compare_cost&gt; open_list; std::unordered_map&lt;int, Node&gt; closed_list; open_list.push(start_node); // main process while (!open_list.empty()) { // pop current node from open list auto current = open_list.top(); open_list.pop(); // current node does not exist in closed list if (closed_list.find(current.id()) != closed_list.end()) continue; closed_list.insert(std::make_pair(current.id(), current)); expand.emplace_back(current.x(), current.y()); // goal found if (current == goal_node) { const auto&amp; backtrace = _convertClosedListToPath&lt;Node&gt;(closed_list, start_node, goal_node); for (auto iter = backtrace.rbegin(); iter != backtrace.rend(); ++iter) { // convert to world frame double wx, wy; costmap_-&gt;mapToWorld(iter-&gt;x(), iter-&gt;y(), wx, wy); path.emplace_back(wx, wy); } return true; } // explore neighbor of current node for (const auto&amp; motion : motions) { // explore a new node auto node_new = current + motion; node_new.set_g(current.g() + motion.g()); node_new.set_id(grid2Index(node_new.x(), node_new.y())); // node_new in closed list if (closed_list.find(node_new.id()) != closed_list.end()) continue; node_new.set_pid(current.id()); // next node hit the boundary or obstacle // prevent planning failed when the current within inflation if ((node_new.id() &lt; 0) || (node_new.id() &gt;= map_size_) || (costmap_-&gt;getCharMap()[node_new.id()] &gt;= costmap_2d::LETHAL_OBSTACLE * obstacle_factor_ &amp;&amp; costmap_-&gt;getCharMap()[node_new.id()] &gt;= costmap_-&gt;getCharMap()[current.id()])) continue; // if using dijkstra implementation, do not consider heuristics cost if (!is_dijkstra_) node_new.set_h(std::hypot(node_new.x() - goal_node.x(), node_new.y() - goal_node.y())); // if using GBFS implementation, only consider heuristics cost if (is_gbfs_) node_new.set_g(0.0); // else, g will be calculate through node_new = current + m ROS_INFO(&quot;*********J-Astar Controller initialized!&quot;); open_list.push(node_new); } } return false; } } // namespace path_planner } // namespace rmp 重写这段代码,要求J_AStarPathPlanner::J_AStarPathPlanner的输入和输出不变,更改评价函数简化成先往一个方向走,然后再考虑转向,点越接近起点或者终点,越优先考虑
09-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值