【问题标题】:How can I print out the images within a vector of strings?如何打印出字符串向量中的图像?
【发布时间】:2020-07-12 17:33:23
【问题描述】:

我在课堂作业方面遇到问题。我们提供了一个网站,该网站在后端有几张各种动物的图片。我们应该能够输入动物的名字,下面会出现与动物名字相对应的图片。这一切都在 search.h 文件和 C++ 中。

教授在讲座中很快就讨论了这个话题,没有太多解释如何继续。我一直在寻找从矢量获取数据的可能解决方案,并尝试使用地图操作但无济于事。我对 C++ 不是很熟悉。我将在下面提供代码。接下来我应该采取什么步骤?

#include <server.h>



ucm::json big5;

std::vector<std::string> lions = {"lion-1.jpeg", "lion-2.jpeg", "lion-3.jpeg", "lion-4.jpeg"};
std::vector<std::string> rhinos = {"rhino-1.jpeg", "rhino-2.jpeg", "rhino-3.jpeg", "rhino-4.jpeg"};
std::vector<std::string> buffalos = {"buffalo-1.jpeg", "buffalo-2.jpeg", "buffalo-3.jpeg", "buffalo-4.jpeg"};
std::vector<std::string> elephants = {"elephant-1.jpeg", "elephant-2.jpeg", "elephant-3.jpeg", "elephant-4.jpeg"};
std::vector<std::string> leopards = {"leopard-1.jpeg", "leopard-2.jpeg", "leopard-3.jpeg", "leopard-4.jpeg"};

void init(){
    big5["lion"] = lions;
    big5["rhino"] = rhinos;
    big5["buffalo"] = buffalos;
    big5["elephant"] = elephants;
    big5["leopard"] = leopards;
}

ucm::json search(std::string key){

    
    // Provide your code here
    // This function should return a specific animal category, specified by key
    // The function should return an appropriate message if the key is not found


}

ucm::json getAll(){
    // This should return all the data we have
    ucm::json temp;
    temp["lion"] = big5["lion"];
    return big5;
}

我再次要求的不是解决方案,而是一个起点。

【问题讨论】:

  • 什么是ucm::json?顺便说一下std::string key 应该是const std::string&amp; key
  • 我相信这就是我访问服务器的原因。 ucm 是我学校的名称。
  • 如果您有权访问ucm::json 文档,您需要检查您是否可以迭代这样的对象。您需要有关该对象允许您执行哪些操作的信息。
  • 你有ucm::json来源的链接吗?
  • 哦,我明白了,我不相信,不,我必须在本地连接到服务器

标签: c++ json


【解决方案1】:

因此,如果我正确理解您的代码,如果您的键是“狮子”,您应该返回包含所有狮子图像的向量。带有狮子图像的向量作为 ucm::json 存储在 big5["lion"] 中。

那么你为什么不检查一下 big5[key] 是否存在,如果存在则返回呢?

所以某种:

ucm::json temp;
if (big5[key]!=NULL){
   temp[key]=big5[key];
   return temp;
} else {
   //print error message here
   return -1;
}

【讨论】:

  • ucm::json的operator[]为什么要返回指针?如果它的行为与 std::map 一样,它将在地图中创建一个新条目。
  • 根据他的 getAll() 函数中的行看起来确实如此。即使没有,正如他所说,他不是在寻求解决方案,而是寻求一个起点。在我看来,搜索存储到 big5 中特定键的向量并返回它是正确的起点。即使它不能与 [] 一起使用,它也可以帮助解决问题。
猜你喜欢
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
相关资源
最近更新 更多