C++ 不可变向量的不可变子向量

C++ 不可变向量的不可变子向量,c++,vector,C++,Vector,假设我有一个const向量,其中包含一些元素,我想创建该向量的const子集,我该怎么做 理想的情况下,如果C++支持下面的代码,那么它就很酷,但不幸的是它没有。有人知道附近有工作吗 #include <fstream> #include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; int main(

假设我有一个
const向量
,其中包含一些元素,我想创建该向量的
const
子集,我该怎么做

理想的情况下,如果C++支持下面的代码,那么它就很酷,但不幸的是它没有。有人知道附近有工作吗

#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    ifstream infile("input.txt"); // contains apple, orange, banana in separate lines
    istream_iterator<string> eos;
    istream_iterator<string> input(infile);
    const vector<string> stuff(input, eos);
    const vector<string> a_stuff(stuff.copy_if([](const string& s) { return s[0] == 'a'; }));

    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
ifstream infle(“input.txt”);//在单独的行中包含苹果、橙色和香蕉
istream_迭代器eos;
istream_迭代器输入(infle);
常量向量填充(输入,eos);
常量向量a_stuff(stuff.copy_if([](常量字符串&s){returns s[0]='a';}));
返回0;
}

假设您想要制作包含子集的副本,您可以使用函数并将结果分配给
常量向量

#包括
#包括
#包括
模板
std::vector vector\u copy\u if(std::vector const&vec、Pred&Pred)
{
std::向量结果;
std::copy_if(vec.begin()、vec.end()、std::back_inserter(result)、pred);
返回结果;
}
int main()
{
向量常数一些_数{1,2,3,4,5,6,7,8,9};
std::vector const偶数=vector拷贝如果(一些偶数,[](int x){返回x%2==0;});
}

假设您想要制作包含子集的副本,您可以使用函数并将结果分配给
常量向量

#包括
#包括
#包括
模板
std::vector vector\u copy\u if(std::vector const&vec、Pred&Pred)
{
std::向量结果;
std::copy_if(vec.begin()、vec.end()、std::back_inserter(result)、pred);
返回结果;
}
int main()
{
向量常数一些_数{1,2,3,4,5,6,7,8,9};
std::vector const偶数=vector拷贝如果(一些偶数,[](int x){返回x%2==0;});
}

也可以使用
boost::filter\u迭代器来获得相同的结果:

#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);
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
常数向量v{“苹果”、“香蕉”、“橙色”};
自动筛选=[](字符串s){返回s.length()>0&&s[0]='a';};
自动启动=boost::生成过滤器迭代器(过滤器,v.begin(),v.end());
auto-end=boost::生成过滤器迭代器(过滤器,v.end(),v.end());
常量向量cv(开始、结束);
}

也可以使用
boost::filter\u迭代器来获得相同的结果:

#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);
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
常数向量v{“苹果”、“香蕉”、“橙色”};
自动筛选=[](字符串s){返回s.length()>0&&s[0]='a';};
自动启动=boost::生成过滤器迭代器(过滤器,v.begin(),v.end());
auto-end=boost::生成过滤器迭代器(过滤器,v.end(),v.end());
常量向量cv(开始、结束);
}

使用子向量将索引存储到原始向量中可能会相当可观吗?使用子向量将索引存储到原始向量中可能会相当可观吗?它有一种骇人听闻的特性,但这是可行的。谢谢。它有一个黑客的品质,但它的工作。谢谢