将文件每一行读入到string类型的vector 对应一个元素中

这篇博客探讨了在C++中如何将文件的每一行读取并存储到一个string类型的vector中。在尝试解决C++ Primer中的第8.9题时,作者遇到了4个编译警告,主要涉及std::reverse_iterator和std::vector的使用。警告发生在D:Program FilesMicrosoft Visual StudioMyProjects标准IO库2标准IO库2.CPP的31行以及包含的头文件中。
摘要由CSDN通过智能技术生成

 

C++Primer 习题8.9, P102

书 P254

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
void main()
{
	string s,filename = "c:/ludashi.txt";
	vector<string> svect;	
	ifstream infile(filename.c_str());
	if(!infile ){
		cerr << "Error opening file";  
		  exit(EXIT_FAILURE);  
	}	
	while(getline(infile,s)){
		   svect.push_back(s);
	}
	infile.close();
	infile.clear();		

	for(vector<string>::iterator it=svect.begin();it!=svect.end();++it)
		cout<<*it<<endl;

}

 

 

出现4个警告warnings ,搞不清楚怎么回事.


D:\Program Files\Microsoft Visual Studio\MyProjects\标准IO库2\标准IO库2.CPP(31) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
假设xyz点云文件格式为每行三个数字,分别是x、y、z坐标,以空格或制表符分隔。 首先,我们需要读入xyz点云文件,并把点云存储到一个vector: ```cpp #include <iostream> #include <fstream> #include <vector> struct Point3D { float x, y, z; }; std::vector<Point3D> readPointCloud(const std::string& filename) { std::vector<Point3D> pointCloud; std::ifstream input(filename); if (!input.is_open()) { std::cerr << "Failed to open file: " << filename << std::endl; return pointCloud; } float x, y, z; while (input >> x >> y >> z) { pointCloud.push_back({x, y, z}); } input.close(); return pointCloud; } ``` 接下来,我们需要实现一个函数,将输入像素坐标形成矩形锚框,并根据锚框切割点云。假设输入的锚框信息为左上角坐标(x1, y1),右下角坐标(x2, y2),则可以按如下方式实现: ```cpp std::vector<Point3D> cropPointCloud(const std::vector<Point3D>& pointCloud, int x1, int y1, int x2, int y2) { // 确保锚框位置合法 if (x1 < 0 || y1 < 0 || x2 >= IMAGE_WIDTH || y2 >= IMAGE_HEIGHT || x1 > x2 || y1 > y2) { std::cerr << "Invalid anchor box: (" << x1 << ", " << y1 << ") - (" << x2 << ", " << y2 << ")" << std::endl; return pointCloud; } // 计算锚框对应的点云索引范围 size_t start = y1 * IMAGE_WIDTH + x1; size_t end = y2 * IMAGE_WIDTH + x2; // 切割点云 return std::vector<Point3D>(pointCloud.begin() + start, pointCloud.begin() + end + 1); } ``` 完整代码如下: ```cpp #include <iostream> #include <fstream> #include <vector> const int IMAGE_WIDTH = 640; const int IMAGE_HEIGHT = 480; struct Point3D { float x, y, z; }; std::vector<Point3D> readPointCloud(const std::string& filename) { std::vector<Point3D> pointCloud; std::ifstream input(filename); if (!input.is_open()) { std::cerr << "Failed to open file: " << filename << std::endl; return pointCloud; } float x, y, z; while (input >> x >> y >> z) { pointCloud.push_back({x, y, z}); } input.close(); return pointCloud; } std::vector<Point3D> cropPointCloud(const std::vector<Point3D>& pointCloud, int x1, int y1, int x2, int y2) { // 确保锚框位置合法 if (x1 < 0 || y1 < 0 || x2 >= IMAGE_WIDTH || y2 >= IMAGE_HEIGHT || x1 > x2 || y1 > y2) { std::cerr << "Invalid anchor box: (" << x1 << ", " << y1 << ") - (" << x2 << ", " << y2 << ")" << std::endl; return pointCloud; } // 计算锚框对应的点云索引范围 size_t start = y1 * IMAGE_WIDTH + x1; size_t end = y2 * IMAGE_WIDTH + x2; // 切割点云 return std::vector<Point3D>(pointCloud.begin() + start, pointCloud.begin() + end + 1); } int main() { std::vector<Point3D> pointCloud = readPointCloud("point_cloud.xyz"); std::vector<Point3D> croppedPointCloud = cropPointCloud(pointCloud, 100, 100, 200, 200); std::cout << "Original point cloud size: " << pointCloud.size() << std::endl; std::cout << "Cropped point cloud size: " << croppedPointCloud.size() << std::endl; return 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值