C++ C++;在向量中查找uint8_t<;uint8_t>;
我有以下简单的代码。在本例中,我声明一个向量并用一个值21初始化它。然后我试着用find在向量中找到那个值。我可以看到这个例子中的元素“21”在向量中,因为我在for循环中打印它。然而,为什么find的迭代器不能解析为trueC++ C++;在向量中查找uint8_t<;uint8_t>;,c++,algorithm,find,C++,Algorithm,Find,我有以下简单的代码。在本例中,我声明一个向量并用一个值21初始化它。然后我试着用find在向量中找到那个值。我可以看到这个例子中的元素“21”在向量中,因为我在for循环中打印它。然而,为什么find的迭代器不能解析为true vector<uint8_t> v = { 21 }; uint8_t valueToSearch = 21; for (vector<uint8_t>::const_iterator i = v.begin(); i != v.end();
vector<uint8_t> v = { 21 };
uint8_t valueToSearch = 21;
for (vector<uint8_t>::const_iterator i = v.begin(); i != v.end(); ++i){
cout << unsigned(*i) << ' ' << endl;
}
auto it = find(v.begin(), v.end(), valueToSearch);
if ( it != v.end() )
{
string m = "valueToSearch was found in the vector " + valueToSearch;
cout << m << endl;
}
向量v={21};
uint8_t valueToSearch=21;
for(vector::const_迭代器i=v.begin();i!=v.end();++i){
你确定它不起作用吗 我刚试过:
#include<iostream> // std::cout
#include<vector>
#include <algorithm>
using namespace std;
int main()
{
vector<uint8_t> v = { 21 };
uint8_t valueToSearch = 21;
for (vector<uint8_t>::const_iterator i = v.begin(); i != v.end(); ++i){
cout << unsigned(*i) << ' ' << endl;
}
auto it = find(v.begin(), v.end(), valueToSearch);
if ( it != v.end() )
{// if we hit this condition, we found the element
string error = "valueToSearch was found in the vector ";
cout << error << int(valueToSearch) << endl;
}
return 0;
}
- 虽然不能在字符串中添加数字,但不能
支持插入运算符(crsn我在google测试中运行此代码,由于某些原因它不会打印。但是在您的回答之后,我刚刚在clean hello world中尝试使用to_字符串(valueToSearch)并打印了它。很抱歉,打扰一下,我可能需要对整个项目进行干净的重建,以解释它:)21 valueToSearch was found in the vector 21