相关疑难解决方法(0)

检查std :: vector是否包含某个对象?

可能重复:
如何在std :: vector中查找项目?

有什么东西<algorithm>可以让你检查std :: container是否包含某些内容?或者,制作一个方法,例如:

if(a.x == b.x && a.y == b.y)
return true;

return false;
Run Code Online (Sandbox Code Playgroud)

这可以只std::map使用密钥吗?

谢谢

c++ vector

245
推荐指数
3
解决办法
42万
查看次数

在C++中检查std :: vector <string>是否包含某个值

是否有内置函数告诉我我的矢量包含某个元素,例如

std::vector<string> v;
v.push_back("abc");
v.push_back("xyz");

if (v.contains("abc")) // I am looking for one such feature, is there any
                       // such function or i need to loop through whole vector?
Run Code Online (Sandbox Code Playgroud)

c++ vector std stdvector

67
推荐指数
4
解决办法
16万
查看次数

如何在一个句子中检查std :: vector中元素的存在?

可能重复:
如何在std :: vector中查找项目?

这就是我要找的东西:

#include <vector>
std::vector<int> foo() {
  // to create and return a vector
  return std::vector<int>();
}
void bar() {
  if (foo().has(123)) { // it's not possible now, but how?
    // do something
  }
}
Run Code Online (Sandbox Code Playgroud)

换句话说,我正在寻找一种简短的语法来验证向量中元素的存在.我不想为这个向量引入另一个临时变量.谢谢!

c++

20
推荐指数
3
解决办法
8万
查看次数

试图使用矢量qsort

我正在尝试学习c ++并尝试使用sort和qsort.sort()工作正常,但qsort没有,我不知道为什么,所以你可以帮助我,这是我试图编译的代码

#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
#include<algorithm>


using namespace std;

int compvar(const void *one, const void *two)
{
    int a = *((int*)one);
    int b = *((int*)two);
    if (a<b)
       return -1;
    if (a == b)
       return 0;
    return 1;   

}

void bvect(vector<int> &vec, int num)
{
     srand(time(NULL));
     for(int i=0; i<num; ++i)
             vec.push_back(rand()%1000 + 1);
}

void showvec(vector<int> vec)
{
     for (int i=0; i<vec.size(); ++i)
         cout<<vec[i]<<endl;
}


int main()
{
    vector<int>numbers;
    bvect(numbers, 1000);
    showvec(numbers);
    qsort(numbers.begin(), numbers.size(), sizeof(int), compvar);
    showvec(numbers);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ sorting vector qsort

15
推荐指数
1
解决办法
3万
查看次数

c ++ std :: vector搜索值

我正在尝试优化std::vector"搜索" - 基于索引迭代向量并返回与"搜索"条件匹配的元素

struct myObj {
   int id;
   char* value;
};

std::vector<myObj> myObjList;
Run Code Online (Sandbox Code Playgroud)

使用唯一id的值和值创建几千个条目并将它们推送到向量myObjList.

什么是最有效的检索方式myObj匹配id.目前我的索引迭代如下:

for(int i = 0; i < myObjList.size(); i++){
   if(myObjList.at(i).id == searchCriteria){
    return myObjList.at(i);
   }
}
Run Code Online (Sandbox Code Playgroud)

注意:searchCriteria = int.所有元素都有独特id之处.上面做的工作,但可能不是最有效的方式.

c++ search iterator vector

11
推荐指数
2
解决办法
3万
查看次数

7
推荐指数
4
解决办法
7369
查看次数

如何检查向量中是否包含值?C++

我有一个向量,我试图执行包含函数.我收到某种铸造错误,我无法拼凑出一个解决方案.我也想知道我在做什么是检查向量是否包含值的适当方法.

这是代码:

#include "stdafx.h"
#include <vector>

static void someFunc(double** Y, int length);
static bool contains(double value, std::vector<double> vec);

int main()
{
    double doubleArray[] = { 1, 2, 3, 4, 5 };
    double *pDoubleArray = doubleArray;
    int size = sizeof doubleArray / sizeof doubleArray[0];

    someFunc(&pDoubleArray, size);

    return 0;
}
static void someFunc(double** Y, int length)
{   
    std::vector<double> vec();

    for(int i = 0; i < 10; i++)
    {
        //error: 'contains' : cannot convert parameter 2 from 'std::vector<_Ty> (__cdecl *)(void)' to 'std::vector<_Ty>'
        if(contains(*(Y[i]), …
Run Code Online (Sandbox Code Playgroud)

c++ casting vector most-vexing-parse

7
推荐指数
2
解决办法
3万
查看次数

用set检测重复项

我正在使用不应弹出两次的数据.如果是,它应该检测它并调用一个处理它的函数.

目前,我正在将一些数据推送到矢量,在插入之前,它应检查数据是否已包含在该矢量中.目前,这不是很有效,例如

for (int i = 0; i < myVector.size() ; i++) 
{
  if ( myVector[i] == data ) 
  {
             // invoke function
             return false;
  }
}
Run Code Online (Sandbox Code Playgroud)

我知道这set是一种特殊的矢量,它只允许唯一的数据.

是否有另一种方法来检测添加(或至少尝试添加)重复数据到set

c++ set

7
推荐指数
3
解决办法
1万
查看次数

算法删除两组交集中的元素

我有一个Visual Studio 2008 C++ 03应用程序,我有两个标准容器.我想从一个容器中删除另一个容器中存在的所有项目(集合的交集).

这样的事情:

std::vector< int > items = /* 1, 2, 3, 4, 5, 6, 7 */;
std::set< int > items_to_remove = /* 2, 4, 5*/;

std::some_algorithm( items.begin, items.end(), items_to_remove.begin(), items_to_remove.end() );

assert( items == /* 1, 3, 6, 7 */ )
Run Code Online (Sandbox Code Playgroud)

是否存在可以执行此操作的现有算法或模式,还是需要自行编写?

谢谢

c++ algorithm

5
推荐指数
1
解决办法
2459
查看次数

热衷于在向量&lt;string&gt;中查找向量&lt;string&gt;元素

我有一些 vector<string> v1,它是使用push_back填充的:

v1.push_back("a");
v1.push_back("b");
v1.push_back("c");
v1.push_back("d");
v1.push_back("e");
Run Code Online (Sandbox Code Playgroud)

我有另一个vector<string> v2和一个迭代器,其中包含一些元素

vector<string>::iterator v2iter;//some element of v2
Run Code Online (Sandbox Code Playgroud)

我需要检查 v2iter 元素是否存在于 v1 中

find(v1, v2iter); //smth like this
Run Code Online (Sandbox Code Playgroud)

c++ vector find

5
推荐指数
1
解决办法
4521
查看次数

标签 统计

c++ ×10

vector ×6

stdvector ×2

algorithm ×1

casting ×1

find ×1

iterator ×1

most-vexing-parse ×1

qsort ×1

search ×1

set ×1

sorting ×1

std ×1

stl ×1