Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 检查字符串是否在将字符串向量作为值的映射的值中_C++_C++11_Stdvector_Stdstring_Stdmap - Fatal编程技术网

C++ 检查字符串是否在将字符串向量作为值的映射的值中

C++ 检查字符串是否在将字符串向量作为值的映射的值中,c++,c++11,stdvector,stdstring,stdmap,C++,C++11,Stdvector,Stdstring,Stdmap,我想检查一个字符串是否在将字符串向量作为值保存的映射的值中 typedef std::map<std::string, std::vector<string>> ClusterDescription; std::map<std::string, std::vector<string>> clusterDescription; std::vector<string> vec1 = {"11", "22", "33"}; std::ve

我想检查一个字符串是否在将字符串向量作为值保存的映射的值中

typedef std::map<std::string, std::vector<string>> ClusterDescription;
std::map<std::string, std::vector<string>> clusterDescription;


std::vector<string> vec1 = {"11", "22", "33"};
std::vector<string> vec2 = {"44", "55"};
std::vector<string> vec3 = {};

std::string key1 = "1";
std::string key2 = "2";
std::string key3 = "3";

clusterDescription.insert(std::make_pair(key1, vec1));
clusterDescription.insert(std::make_pair(key2, vec2));
clusterDescription.insert(std::make_pair(key3, vec3));

std::string ID = "44";

for (ClusterDescription::iterator it = clusterDescription.begin(); it != clusterDescription.end(); ++it)
{
    std::vector<std::string> clusterMembers = it->second;
    if(std::find(clusterMembers.begin(), clusterMembers.end(), ID) != clusterMembers.end())
    {
        std::cout<< " I received an msg, from the wrong head "<< std::endl;      //FIXME:
        break;
    }
    else
    {
        std::cout<< " I have not been included in any cluster yet "<< std::endl;        //FIXME:
        std::cout<< " sending joinmode msg "<< std::endl;
        break;
    }
}
typedef std::map ClusterDescription;
地图聚类描述;
向量vec1={“11”,“22”,“33”};
向量vec2={“44”,“55”};
std::vector vec3={};
std::string key1=“1”;
std::string key2=“2”;
std::string key3=“3”;
clusterDescription.insert(std::make_pair(key1,vec1));
clusterDescription.insert(std::make_pair(key2,vec2));
clusterDescription.insert(std::make_pair(key3,vec3));
std::string ID=“44”;
for(ClusterDescription::iterator it=ClusterDescription.begin();it!=ClusterDescription.end();+it)
{
std::vector clusterMembers=it->second;
if(std::find(clusterMembers.begin(),clusterMembers.end(),ID)!=clusterMembers.end())
{

std::cout那么您想知道是否可以在集群中的任何向量中找到字符串?请使用标准算法
any\u
find

bool stringIsContained(const ClusterDescription &cluster, const std::string &s)
{
  return std::any_of(
    begin(cluster), end(cluster), [&s](ClusterDescription::const_reference item)
    {
      return std::find(begin(item.second), end(item.second), s) != end(item.second);
    }
  );
}

使用标准算法尽可能是惯用的C++。


要解决原始代码无法按您希望的方式工作的原因,请执行以下操作:

在条件的两个分支中都有一个
break
,这意味着循环体将只对集群中的第一个字符串向量对执行

如果您只是删除中断,它将对集群中的每个字符串向量对执行一次。因为每个分支都有一个输出语句,所以您将为每个这样的对获得一个输出语句


如果您打算搜索整个数据结构,并且只在最后输出,那么您必须将搜索结果存储在某个地方,并且只在循环外输出。这与我上面的代码所做的差不多,只是循环隐藏在算法中。结果是“存储的”在返回值中,您只需对函数的最终返回值执行一次操作。

在第一次迭代后,您将打破for循环,因此只分析映射的第一个元素。@cross:为什么您不期望它呢?这就是您的代码所做的,我们所能看到的只是您的代码,而不是您想要的代码t因此,通过阅读你的意图是不可能预知你的意图的。