我想让我的迭代器到最后一次出现1。
例如,在向量A[0, 0, 1, 0, 1, 0, 1, 0, 0]
中,我希望在基于0的索引中将迭代器提高到第六位。我想使用向量函数来执行此操作,但是该数组不会在程序中进行硬编码。该程序将从用户那里获取一个由0和1组成的向量,然后使迭代器最后一次出现1,谢谢!
从逻辑上讲,我想做这样的事情来获取用户的输入:
std::vector<int> b, c;
for (auto d : b)
std::cin >> c;
b.push_back(c);
答案 0 :(得分:0)
您可以使用std::upper_bound()
查找索引,但需要对数组进行排序。
或者,您可以将数组转换为字符串,然后使用std::string::find()
查找元素。
答案 1 :(得分:0)
要读入数字直到出现非数字内容,您需要循环测试>>
std::vector<int> a;
for (int b; std::cin >> b;) { a.push_back(b); }
然后您可以使用std::find
s来std::reverse_iterator
auto it = std::find(a.rbegin(), a.rend()).base();
// showing the index
std::cout << std::distance(a.begin(), it);
答案 2 :(得分:0)
我建议您使用另一个向量的参数进行向量功能。
喜欢
ForEach
要将用户的价值接受到向量中,您可以做的是-
vector<int>Printvectors(vector<int>vectornew){} // If you want the return type to be
// a vector
并使用下限函数,我建议矢量将迭代器放置在所需位置。使用vector<int>a;
while(cin>>i){
a.push_back(i);
}
a.sort(a.begin(),a.end()); // to sort
vector<int>Printvectors(vector<int>a);
查找元素。
答案 3 :(得分:-1)
我建议您使用类似这样的内容:
for(int i=0;i<array.length;i++){
array[i]=cin<<"use user input here"
}
对于代码错误,请向我道歉,但自4年以来我不再使用c ++