C++ string与vector<float>类型相互转换之stringstream(十九)

1.代码示例  

1.stringstream类型转换用法
streamstring在调用str()时,会返回临时的string对象。而因为是临时的对象,所以它在整个表达式结束后将会被析构。
如果需要进一步操作string对象,先把其值赋给一个string变量后再操作。
//sample
stringstream ss("012345678901234123456789");  
//错误  
const char* cstr2 = ss.str().c_str();  
//正确  
const string& str2 = ss.str();  
const char* cstr2 = str2.c_str();  
//stringstream清空缓冲区
stringstream stream(“abcdefghi”);
stream.str(“”);

2.string转vector<float>类型
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;
//1.string类型转vector<float>类型
void string_to_vector_float(std::vector<float>& fea){
  string str = "-0.0647031 -0.0206785 -0.0439941 -0.0323903 -0.0139745 -0.0208939 0.0289745 0.0277657 -0.0807781 -0.0288254";
  stringstream ss(str);
  string buf;
  vector<float> vec;
  //vector<float> vec(1024);//可以初始化时指定vecrot容器大小
  while(ss >> buf)
    vec.push_back(atof(buf.c_str()));

  cout <<"vector容器大小: "<< vec.size() << endl;
  cout <<"vector容器容量: "<< vec.capacity() << endl;
  
  fea = vec;
}

int main()
{
  vector<float> vec;

  string_to_vector_float(vec);
  
  //1.C++11 usage:打印结果
#if 0
  for(auto ff : vec)
    cout << ff << endl;
#endif

//2.打印结果
  for(int i = 0; i < vec.size(); i++){
    if(i < 255)
      cout  << vec[i] << " " ;
    else if(i == 255)
      cout  << vec[i] ;
  }
  cout  << endl;
  
  return 0;
}

3.vector<float>转string类型
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cassert>
#include "json/json.h"
#include <vector>
using namespace std;

int ReadFromFile(string fileName)
{
  ifstream fin;
  fin.open(fileName);
  assert(fin.is_open());

  Json::Reader reader;
  Json::Value  root;
  if(reader.parse(fin, root, false) != true)
    return -1;

  cout << "arraySize = " << root["name"].size() << endl;
  cout << "my name is:" << root["name"] << endl;
}

int WriteToFile(string fileName)
{
  ofstream fout;
  fout.open(fileName);
  assert(fout.is_open());
  Json::Value root;
  //root["array"]["array1"]["name"] = "xiaozhao";
  root["name"] = "xiaozhao";
  fout << root.toStyledString() << endl;
  fout.close();
  return 0;
}

void vec2string(vector<float> &feature, string &f_data){
  std::stringstream ss;
  for(int i = 0; i < 15; i++){
    feature.push_back(i);
  }
  cout << "v.size() = " << feature.size() << endl;
  for(size_t i = 0; i < feature.size(); ++i)
  {
    if(i != 0)
      ss << " ";
    ss << feature[i];
  }
  
  f_data = ss.str();
  cout << "ss = " << ss.str() << endl;
  ss.str(“”);//使用同一个stringstream对象,需要使用其对象方法str来重置stringstream的buffer,这样能确保stringstream占用的内存不会越积越多。
  cout << "ss = " << ss.str() << endl;
}

int main()
{
   string str;
  //WriteToFile("test.json");
  //ReadFromFile("test.json");
  vector<float> fea;
  vec2string(fea, str);
  cout << “str = “ << str << endl;

  return 0;
}

Json for C++库下载:
# git clone https://gitee.com/Tocy/SampleCode

#include <ros/ros.h> #include "Utils/param.h" #include "control.hpp" #include <sstream> namespace ns_control { Param control_param_; Control::Control(ros::NodeHandle &nh) : nh_(nh) { controller_ = nh_.param<std::string>("controller", "pure_pursuit"); control_param_.getParams(nh_, controller_); if (controller_ == "pure_pursuit") { solver_ = &pure_pursuit_solver_; } else if (controller_ == "mpc") { solver_ = &mpc_solver_; } else { ROS_ERROR("Undefined Solver name !"); } } void Control::setCarState(const fsd_common_msgs::CarState &msgs) { car_state_ = msgs; } void Control::setTrack(const Trajectory &msgs) { refline_ = msgs; } fsd_common_msgs::ControlCommand Control::getCmd() { return cmd_; } visualization_msgs::MarkerArray Control::getPrePath() { return PrePath_; } bool Control::Check() { if (refline_.empty()) { ROS_DEBUG_STREAM("Successfully passing check"); return false; } return true; } void Control::runAlgorithm() { if (!Check()) { ROS_WARN_STREAM("Check Error"); return; } solver_->setState(VehicleState(car_state_, cmd_)); solver_->setTrajectory(refline_); solver_->solve(); cmd_ = solver_->getCmd(); std::vector<float> color_ref = {1, 0, 0}; std::vector<float> color_pre = {0, 1, 0}; std::vector<float> color_init = {0, 0, 1}; if (controller_ == "mpc") visual_trajectory(solver_->getTrajectory(), PrePath_, "/base_link", color_pre, car_state_.header, true); std::cout << "steering: " << cmd_.steering_angle.data << std::endl; std::cout << "throttle: " << cmd_.throttle.data << std::endl; }翻译这段代码
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android系统攻城狮

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值