【问题标题】:List of List C++列表 C++ 列表
【发布时间】:2014-10-24 09:13:49
【问题描述】:

我已经创建了一个从图像中获得的点列表。但是我有很多图像的问题,所以我想创建一个列表列表。我的想法是创建一个字符串列表(包含图像名称)并将点列表添加到该列表中(字符串列表的每个图像都包含该图像的点列表)。我不知道该怎么做。这是我第一次操作列表。

这是我到目前为止所做的:

struct Point
{
    double x, y;
};
list<Point> landmarks;
list<string> image_name;

for (loop over the images that i have in my folder)
{
    for (loop over point in every image in the folder image)
    {
        Point p;
        p.x = it_shapeROI->roi.pXmin;
        p.y = it_shapeROI->roi.pYmin;
        landmarks.push_back( p ); // list of point in one image
    }
     // Here I want to add to the list of string the list of point.
     // So I will have at the end a list of string (image name)
     // that contain for every name a list of point of that images.
}

【问题讨论】:

    标签: c++ list stl


    【解决方案1】:

    也许,结合矢量的地图适合您的目标:

    #include <vector>
    #include <map>
    #include <string>
    
    class Point
    {
    	public:
    		Point(int x, int y) : _x(x), _y(y){}
    	private:
    		int _x;
    		int _y;
    };	
    
    int main(void) 
    {
    	std::map<std::string, std::vector<Point> > mymap;
    	for (int i=0; i<10; ++i) // Loop over images
    	{
    		std::vector<Point> pts;
    		pts.reserve(10);
    		for (int j=0; j<10; ++j) // Loop over points from image i
    		{
    			pts.push_back(Point(i,j));
    		}
    		mymap.insert(std::pair<std::string, std::vector<Point> >("filename", pts));
    	}	
    }

    【讨论】:

    • 为什么要从 main 返回 1,只是为了与众不同?
    • 只是为了与默认语句不同返回0(没想太多)。我删除了 return 语句以避免混淆
    • @JanFischer 如果您的程序因错误而关闭,则 main() 的返回值会告诉程序调用您的程序。返回 1;这意味着您的程序遇到了一个错误,导致它停止。 return 0 表示你的程序没有错误结束。
    【解决方案2】:

    我想最好将您的数据安排在std::map 中。 (cppreference.com)

    std::map<std::string, std::vector<Point>> imageMap;
    

    然后您需要遍历所需文件夹中的所有图像(使用底层操作系统的 API)并为每个图像创建一个点列表(并将其插入到地图中,文件名作为相应的键)。

    【讨论】:

      【解决方案3】:

      嗯,怎么样

      struct image
      {
        char *name;
        std::vector<Point> coords;
      }
      
      std::vector<image> imageList;
      

      【讨论】:

      • 如果您不知道字符串还有什么用途,那么使用 char* 或 std::string 真的很重要吗?
      • 谁删除了字符串?它是字符串文字吗?字符串会被修改吗?实际上,当您不知道字符串将用于什么时,请使用 std::string。
      【解决方案4】:

      #include <vector>
      #include <map>
      #include <string>
      
      class Point
      {
      	public:
      		Point(int x, int y) : _x(x), _y(y){}
      	private:
      		int _x;
      		int _y;
      };	
      
      int main(void) 
      {
      	std::map<std::string, std::vector<Point> > mymap;
      	for (int i=0; i<10; ++i) // Loop over images
      	{
      		std::vector<Point> pts;
      		pts.reserve(10);
      		for (int j=0; j<10; ++j) // Loop over points from image i
      		{
      			pts.push_back(Point(i,j));
      		}
      		mymap.insert(std::pair<std::string, std::vector<Point> >("filename", pts));
      	}	
      }

      感谢您的帮助。我试着知道从我的列表中获取要点以在其他功能中使用它:

      for( map<string, vector<Point> >::iterator iter = mymap.begin(); iter != mymap.end(); ++iter )
                      {
                          vector<Point> tempVec = (*iter).second;
                          string Key = (*iter).first;
                          cout << Key;
                          for (unsigned i = 0; i < tempVec.size(); i++)
                          {
                              cout << Key << "    " << tempVec[i].x  << endl;
                          }
                      cout << endl;
                      }
      

      我不工作,似乎是因为他每次都给我0。

      【讨论】:

        猜你喜欢
        • 2013-12-18
        • 1970-01-01
        • 2016-06-14
        • 2015-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-05
        • 1970-01-01
        相关资源
        最近更新 更多