Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.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++_Vector_Binary Search - Fatal编程技术网

C++ C++;向量的多条件二元搜索

C++ C++;向量的多条件二元搜索,c++,vector,binary-search,C++,Vector,Binary Search,所以我在做一个赋值,我需要为一个有4个变量的对象搜索一个向量。 字符串,字符串,int,int。我对编程很陌生。如果我只使用第一个字符串进行搜索,那么二进制serach就可以工作了。但不确定如何使其匹配所有四个字段 向量按第一个字符串排序,如果第一个字符串匹配,则按第二个字符串排序,如果第二个字符串匹配,则按第一个int排序,等等 到目前为止,我的代码是 bool Room::searchRoom(string name, string initial, int number1, int num

所以我在做一个赋值,我需要为一个有4个变量的对象搜索一个向量。 字符串,字符串,int,int。我对编程很陌生。如果我只使用第一个字符串进行搜索,那么二进制serach就可以工作了。但不确定如何使其匹配所有四个字段

向量按第一个字符串排序,如果第一个字符串匹配,则按第二个字符串排序,如果第二个字符串匹配,则按第一个int排序,等等

到目前为止,我的代码是

bool Room::searchRoom(string name, string initial, int number1, int number2) {

    size_t mid, left = 0;
    size_t right = testVector.size();
    while (left < right) {
        mid = left + (right - left) / 2;
        if (name > testVector[mid].getName()) {
            left = mid + 1;
        } else if (name < testVector[mid].getName()) {
            right = mid;
        } else {
            return true;
        }
        return false;
    }
}
boolroom::searchRoom(字符串名称、字符串首字母、整数1、整数2){
中间尺寸,左=0;
size\u t right=testVector.size();
while(左<右){
中间=左+(右-左)/2;
如果(name>testVector[mid].getName()){
左=中+1;
}else if(name
就像讲故事的人说的那样。下面是如何将第二个变量添加到比较中,我让您执行第三个和第四个

while (left < right) {
  mid = left + (right - left)/2;
  if (name > testVector[mid].getName()) {
      left = mid+1;
  }
  else if (name < testVector[mid].getName()) {
      right = mid;
  }
  else if (initial > testVector[mid].getInitial()) {
      left = mid+1;
  }
  else if (initial < testVector[mid].getInitial()) {
      right = mid;
  }
  ... // third and fourth variables here
  else {
      return true;
  }
}
while(左<右){
中间=左+(右-左)/2;
如果(name>testVector[mid].getName()){
左=中+1;
}
else if(nametestVector[mid].getInitial()){
左=中+1;
}
else if(initial
就像讲故事的人说的那样。下面是如何将第二个变量添加到比较中,我让您执行第三个和第四个

while (left < right) {
  mid = left + (right - left)/2;
  if (name > testVector[mid].getName()) {
      left = mid+1;
  }
  else if (name < testVector[mid].getName()) {
      right = mid;
  }
  else if (initial > testVector[mid].getInitial()) {
      left = mid+1;
  }
  else if (initial < testVector[mid].getInitial()) {
      right = mid;
  }
  ... // third and fourth variables here
  else {
      return true;
  }
}
while(左<右){
中间=左+(右-左)/2;
如果(name>testVector[mid].getName()){
左=中+1;
}
else if(nametestVector[mid].getInitial()){
左=中+1;
}
else if(initial
您可以添加
运算符您可以添加
运算符如果第一个字符串匹配,则比较第二个字符串,如果这些字符串匹配,则比较第一个整数。整体比较的结果应该是最后一次不相等的比较。大括号不匹配。我建议您将二进制搜索逻辑和比较两个不同功能房间的逻辑分开。这会容易得多。因此,如果第一个字符串匹配,则比较第二个字符串,如果这些字符串匹配,则比较第一个整数。整体比较的结果应该是最后一次不相等的比较。大括号不匹配。我建议您将二进制搜索逻辑和比较两个不同功能房间的逻辑分开。这会容易得多。谢谢你。答案比我想象的要简单得多,哈哈。我想到了带有多个&&and | | |或一些戏剧性的巨大的反常if语句。谢谢你。答案比我想象的要简单得多,哈哈。我想到了带有多个&&and | | |或一些戏剧性的巨大的反常if语句。