包含头文件:string.h
一、数字转string,这些都可以
std::to_string(int)
std::to_string(long)
std::to_string(long long)
std::to_string(float)
std::to_string(double)
std::to_string(long double)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num=123;
string str=to_string(num);
cout<<str<<endl;
return 0;
}
二、string转数字
头文件:cstdlib
std::stoi
std::stol
std::stoll
std::stod
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num=0;
string str="123";
num=stoi(str);
cout<<num<<endl;
return 0;
}
三、具体例子
假设txt 数据格式如下:
我们下面的程序是 一行一行数据的读取,也可以整文件的读取:
int main(int argc, char** argv)
{
ros::init(argc, argv, "vins_estimator");
ros::NodeHandle n("~");
ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Info);
pubGPS = n.advertise<sensor_msgs::NavSatFix>("/gps", 1000);
if(argc != 3)
{
printf("please intput: rosrun vins camera_IMU_GPS_test [config file] [data folder] \n"
"for example: rosrun vins camera_IMU_GPS_test "
"~/catkin_ws/src/VINS-Fusion/config/test_data/stereo_imu_gps_config.yaml "
"/home/hltt3838/kitti_data/2011_10_03_drive_0042_sync/ \n");
return 1;
}
string config_file = argv[1];//stereo_imu_gps_config.yaml
printf("config_file: %s\n", argv[1]);
string sequence = argv[2];//---/home/hltt3838/kitti_data/2011_10_03_drive_0042_sync/
printf("read sequence: %s\n", argv[2]);
string dataPath = sequence + "/";
//读取imu的 txt 文件
IMU_MSG imuObs;
std::string line_imu;
std::string imuPath = dataPath + "imu_data_100hz/imu.txt";
std::ifstream csv_IMUfile(imuPath);
if (!csv_IMUfile)
{
printf("cannot find imu Path \n" );
ROS_BREAK();
return 0;
}
std::getline(csv_IMUfile, line_imu);//header, 获得的第一个IMU数据
readIMUdata(line_imu, imuObs);
// std::cout 有效位数 的设置,包括头文件 #include <iomanip>
std::cout << "imu time 0:" << setprecision(10) << imuObs.t_time << std::endl;
std::cout << "imuObs.acc 0:" << setprecision(10) << imuObs.acc[0] << std::endl;
printf("printf imu time : %10.5f \n", imuObs.t_time); // printf 有效位数 的设置
while (std::getline(csv_IMUfile,line_imu))
{
readIMUdata(line_imu,imuObs);
std::cout << "imu time" << imuObs.t_time << std::endl;
}
return 0;
}
其中:void readIMUdata(const std::string &line, IMU_MSG &imuObs);
struct IMU_MSG
{
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
double t_time;
Eigen::Vector3d acc;
Eigen::Vector3d gyr;
IMU_MSG &operator =(const IMU_MSG &other)
{
t_time = other.t_time;
acc = other.acc;
gyr = other.gyr;
return *this;
}
};
///
void readIMUdata(const std::string &line, IMU_MSG &imuObs)
{
std::stringstream lineStream(line);
std::string dataRecord[7];
std::getline(lineStream, dataRecord[0], ' ');//这里的数据间是空格, 如果有逗号,用','
std::getline(lineStream, dataRecord[1], ' ');
std::getline(lineStream, dataRecord[2], ' ');
std::getline(lineStream, dataRecord[3], ' ');
std::getline(lineStream, dataRecord[4], ' ');
std::getline(lineStream, dataRecord[5], ' ');
std::getline(lineStream, dataRecord[6], ' ');
imuObs.t_time = std::stod(dataRecord[0]); //时间:s;
imuObs.acc[0] = std::stod(dataRecord[1]);
imuObs.acc[1] = std::stod(dataRecord[2]);
imuObs.acc[2] = std::stod(dataRecord[3]);
imuObs.gyr[0] = std::stod(dataRecord[4]);
imuObs.gyr[1] = std::stod(dataRecord[5]);
imuObs.gyr[2] = std::stod(dataRecord[6]);
}