代码片段笔记-提高编程效率

classic code

代码调试的临时代码管控

.h
typedef enum
{
  release_state = 0,
  debug_state,//调试阶段和非调试阶段的分界线
  real_team_debug = debug_state,
  real_team_debug_single_gps_device,
  simulate_nodes_debug,
  coding_debug,//解决基本代码是否有问题,是否能够联通gps设备,是否有数据上来的级别的bug
  max_state = coding_debug,
}Debug_state;//调试节奏是从下往上

int CODE_STATE  = simulate_nodes_debug;

.cpp
if (CODE_STATE >= simulate_nodes_debug)
    cout << "in :" << endl;

std

iterator的使用

std::vector<track_data> track_datas_t;

for (vector<track_data>::iterator it = track_datas_t.begin(); it != track_datas_t.end(); ++it)
      { 
        cout << "it is:" << endl;
        gps_data_ht_.odom.pose.pose.position.x = it->x;
        gps_data_ht_.odom.pose.pose.position.y = it->y;

        exhibition_odom_publisher_.publish(gps_data_ht_);
      }

include boost

#include <boost/tokenizer.hpp>
#include <boost/thread/thread.hpp>

catkin_package(
      INCLUDE_DIRS include
      LIBRARIES novatel
      CATKIN_DEPENDS serial roslib roscpp rosconsole tf gps_msgs nav_msgs sensor_msgs
      DEPENDS Boost
    )

find_package(Boost REQUIRED COMPONENTS system filesystem thread)

find_package(Boost REQUIRED COMPONENTS
        thread
      )


thread&boost

boost::thread

.cpp
read_thread_ptr_ = boost::shared_ptr<boost::thread>(new boost::thread(boost::bind(&Novatel::send_rest_locate_data_frq_func, this)));
.h
boost::shared_ptr<boost::thread> read_thread_ptr_;
void send_rest_locate_data_frq_func();

pthread

pthread_create

.cpp
if (!tid)
pthread_create(&tid, NULL, CAN_rcv_handler, this);
void *Driver_CAN::CAN_rcv_handler(void *arg)
{}
.h
static void *CAN_rcv_handler(void *arg);//in .h

condition&boost

.cpp
ack_condition_.notify_all();
            
boost::mutex::scoped_lock lock(ack_mutex_);
boost::system_time const timeout = boost::get_system_time() + boost::posix_time::milliseconds(2000);
if (ack_condition_.timed_wait(lock, timeout))
.h
boost::condition_variable ack_condition_;

time

//获取当地时间
time_t current_time;
time(&current_time);
local_time = localtime(&current_time);
//获取微妙数据
struct timeval tv;
struct timezone tz;
gettimeofday(&tv, &tz);
std::cout << "["
    << local_time->tm_year + 1900 << "-"
    << local_time->tm_mon + 1 << "-"
    << local_time->tm_mday << " "
    << local_time->tm_hour << ":"
    << local_time->tm_min << ":"
    << local_time->tm_sec << "."
    << tv.tv_usec << "]"
    << ", "
    << " [x]:" << gps_data_ht_.odom.pose.pose.position.x - Novatel::x_zero << ","
    << " [y]:" << gps_data_ht_.odom.pose.pose.position.y - Novatel::y_zero << ","
    << " [z]:" << gps_data_ht_.odom.pose.pose.position.z
    << " [heading]:" << gps_data_ht_.heading
    << " [velocity]:" << gps_data_ht_.velocity
    << " [x_zero]:" << Novatel::x_zero << ","
    << " [y_zero]:" << Novatel::y_zero << ","
    << std::endl;

分割字符串到vector中
1、自定义函数

// stolen from: http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html
void Tokenize(const std::string &str, std::vector<std::string> &tokens, const std::string &delimiters = " ")
{
    // Skip delimiters at beginning.
    std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    std::string::size_type pos = str.find_first_of(delimiters, lastPos);

    while (std::string::npos != pos || std::string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}

std::string log_string
std::vector<std::string> logs;
//在log_string中以 分号作为分隔符 搜索所有的内容
//将内容放入vector logs中
Tokenize(log_string, logs, ";");

2、boost的自带的方法

typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
    boost::char_separator<char> sep(" ");

    while(getline(track_file,line))//会自动把\n换行符去掉 
    {
      tokenizer tokens(line, sep);
      tokenizer::iterator current_token = tokens.begin();
      track_data_temp.num = atof((*(current_token++)).c_str());
      track_data_temp.x = atof((*(current_token++)).c_str());
      track_data_temp.y = atof((*(current_token)).c_str());
      cout << "num is:" << track_data_temp.num << endl;
      cout << "lati is:" << track_data_temp.x << endl;
      cout << "longi is:" << track_data_temp.y << endl;
      track_datas_t.push_back(track_data_temp);
      cout << "track_datas_t size is:" << track_datas_t.size() << endl;
    }

stream

file stream

get file into buff

std::string track_file_path_ = "/home/yuhs/track.txt";
std::ifstream track_file(track_file_path_.c_str(), std::ios::binary);

if (!track_file.is_open())
{
std::cout << "track File " << track_file_path_ << " did not open." << std::endl;
}

//获取文件大小
track_file.seekg(0, track_file.end);
size_t size = track_file.tellg();

//Put data into buffer
unsigned char buff[size];

//重新将读取指针定位到begin
track_file.seekg(0, track_file.beg);
track_file.read((char *)buff, size);

// Finished with the file, close it
track_file.close();


//获取到文件的每一行
vector<string>  strings;
  string  line;
  while(getline(track_file,line))//会自动把\n换行符去掉 
  {
      strings.push_back(line);
  }
  cout << "string nums is:" << strings.size() << endl;

string

转换

string和各种数据的相互转换

string a = "1234";
int b = atoi(a.c_str());

ros

set

set(hahaha  "hahahah")

topic

ros::Publisher exhibition_odom_publisher_;

this->exhibition_odom_publisher_ = nh_.advertise<gps_msgs::Gps_Data_Ht>(exhibition_odom_topic_, 0);
ros::Subscriber cmd_sub = n.subscribe("/cmd_vel", 1000, cmd_callback);

void cmd_callback(const geometry_msgs::Twist &vel)
{

    if (cmd_manual == 0)
    {
        cout << "cmd_vel rcved\n"
             << endl;
        linear_v = vel.linear.x;
        angular_w = vel.angular.z;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容