我们说我有一个$('#fullpage').fullpage({
sectionsColor: ['yellow', 'orange', '#C0C0C0', '#ADD8E6'],
fitToSection:false,
//events
onLeave: function(index, nextIndex, direction){
if (nextIndex ===1){
$.fn.fullpage.setAutoScrolling(true);
}
else if(nextIndex ===2){
$.fn.fullpage.setAutoScrolling(false);
}
},
afterLoad: function(anchorLink, index){},
});
,其中包含一些元素,我想创建该向量的const vector<string>
子集,我该怎么做?
理想情况下,如果C ++支持类似下面代码的内容会很酷,但不幸的是它并不是。有人知道一项工作吗?
const
答案 0 :(得分:2)
假设您要制作包含子集的副本,您可以使用函数并将结果分配给const vector<T>
#include <vector>
#include <algorithm>
#include <iterator>
template <typename T, typename Pred>
std::vector<T> vector_copy_if(std::vector<T> const & vec, Pred && pred)
{
std::vector<T> result;
std::copy_if(vec.begin(), vec.end(), std::back_inserter(result), pred);
return result;
}
int main()
{
std::vector<int> const some_numbers { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::vector<int> const even_numbers = vector_copy_if(some_numbers, [](int x) { return x % 2 == 0; });
}
答案 1 :(得分:1)
也可以使用#include <vector>
#include <string>
#include <boost/iterator/filter_iterator.hpp>
using namespace std;
int main()
{
const vector<string> v {"apple", "banana", "orange"};
auto filter= [] (string s){return s.length() > 0 && s[0] == 'a';};
auto start = boost::make_filter_iterator(filter, v.begin(), v.end());
auto end = boost::make_filter_iterator(filter, v.end(), v.end());
const vector<string> cv (start, end);
}
来获得相同的结果:
register_params