C/C++ 如何判断vector中某值 是否存在!!

//gogo[0][4]根本就不存在,但我如何判断??
vector<string> run;
vector< vector<string> > gogo;
run.push_back("123");run.push_back("123");run.push_back("123");
gogo.push_back(run);
cout<<gogo[0][4].length();
if(gogo[0][4].length()==0)
{
cout<<gogo[0][2];
cout<<"yes";
}
上代码最实际。size()可以我早就结贴了。。。

gogo[0][4]根本就未申明,我想直接判断gogo[0][4]是否存在,而不是先去计算gogo[0]的个数或run的个数。

#include <algorithm>

vector<int> vec;

        vec.push_back(1);

        vec.push_back(2);

        vec.push_back(3);

        vec.push_back(4);

        vec.push_back(5);

        vector<int>::iterator ret;

        ret = std::find(vec.begin(), vec.end(), 15);

        if(ret == vec.end())

            cout << "not found" << endl;

        else

            cout << "found it" << endl;

扩展资料:  

C/C++ —语言判断数字或字符的函数

isalnum函数

原型:extern int isalnum(int c);  

用法:#include <ctype.h>   

功能:判断字符变量c是否为字母或数字  

说明:当c为数字0-9或字母a-z及A-Z时,返回非零值,否则返回零 

isupper函数 

原型:extern int isupper(int c);

头文件:(旧版本的编译器使用<ctype.h>)

功能:判断字符c是否为大写英文字母

说明:当参数c为大写英文字母(A-Z)时,返回非零值,否则返回零。

附加说明: 此为宏定义,非真正函数。

isgraph函数

定义函数:int isgraph (int c)

函数说明:检查参数c是否为可打印 字符,若c所对映的ASCII码可打印,且非空格字  符则返回TRUE。

返回值:若参数c为可打印字符,则返回TRUE,否则返回NULL(0)。

附加说明:此为 宏定义,非真正函数。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-22

方法一:自己写循环遍历。

方法二:使用STL的find()函数。

用法例子:

vector<int> vec;
vector<int>::iterator it;
int value=24;

it=find(vec.begin(),vec.end(),value);

if (it!=vec.end())
{
//vec中存在value值
}
else
{
//vec中不存在value值
}

注意,如果vector中保存的是自定义类型(结构体/类),则需要为该类型重载==操作符。

第2个回答  2011-04-12
不是很明白你的意思 是说gogo[0][4]的那个边界你自己知道的吗,然后用一个越界的数进行测试之类的吗 ??
按照我的理解 就是先取出size ,然后拿那个边界和size比较
#include<iostream>
#include<cstdlib>
#include<vector>
#include<string>
using namespace std;

int main()
{
vector<string> run;
vector< vector<string> > gogo;
run.push_back("123");run.push_back("123");run.push_back("123");
gogo.push_back(run);
int size = gogo.size();
if(size>=4){
cout<<gogo[0][4].length();

if(gogo[0][4].length()==0)
{
string temp = gogo[0][2];
cout <<temp<<endl;
cout << "yes";
}
}

else
printf("越界了!");
return 0;
}
第3个回答  2011-04-11
你可以先找gogo[0].size()求其内所存实际数据个数,然后可以知道gogo[0][4]是不是存在了
第4个回答  推荐于2017-10-11
1. 像数组一样使用,size获取大小,使用下标来访问处理。
2. 使用iterator来处理,到最后是,迭代器为空了。

参考资料:stl手册

本回答被提问者采纳
相似回答