std::vector,std::list并且std::deque有std::back_inserter和std::set有std::inserter.
对于std::stack和std::priority_queue我会假设等效的插入器将是一个,push()但我似乎无法找到正确的函数来调用.
我的意图是能够使用以下函数和正确的插入迭代器:
#include <string>
#include <queue>
#include <iterator>
template<typename outiter>
void foo(outiter oitr)
{
static const std::string s1 ("abcdefghji");
static const std::string s2 ("1234567890");
*oitr++ = s1;
*oitr++ = s2;
}
int main()
{
std::priority_queue<std::string> spq;
std::stack<std::string> stk;
foo(std::inserter(spq));
foo(std::inserter(stk));
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在std::back_insert_iterator具有value_type等于void,但它也有一个protected构件container,其保持指针到底层Container.我试图写一个traits类来提取容器value_type,沿着这些方向:
#include <iterator>
#include <type_traits>
#include <vector>
template<class OutputIt>
struct outit_vt
:
OutputIt
{
using self_type = outit_vt<OutputIt>;
using value_type = typename std::remove_pointer_t<decltype(std::declval<self_type>().container)>::value_type;
};
int main()
{
std::vector<int> v;
auto it = std::back_inserter(v);
static_assert(std::is_same<outit_vt<decltype(it)>::value_type, int>::value, "");
}
Run Code Online (Sandbox Code Playgroud)
但是,这(或多或少地预期)会遇到不完整的类型错误.反正这是为了得到容器的提取物value_type吗?
back_inserter非常insert_iterator方便,但效率也很低!
例如,当您追加chars 时,每个元素都会产生大量开销copy,而在许多情况下,实际上并不需要如此。
有没有办法让他们更有效率?
我正在编写一个代码,其中一个文件使用set_intersection函数,该函数的最后一个参数应该是一个插入器.但是当我编译代码时,我看到以下错误:
error C2039: 'inserter' : is not a member of 'std'
error C2873: 'inserter' : symbol cannot be used in a using-declaration
error C3861: 'inserter': identifier not found
Run Code Online (Sandbox Code Playgroud)
以下是使用set_intersection函数的文件中的代码
#include "Query.h"
#include "TextQuery.h"
#include <set>
#include <algorithm>
#include <iostream>
using std::set;
using std::ostream;
using std::inserter;
using std::set_intersection;
// returns intersection of its operands' result sets
set<TextQuery::line_no>
AndQuery::eval(const TextQuery& file) const
{
// virtual calls through the Query handle to get result sets for the operands
set<line_no> left = lhs.eval(file),
right …Run Code Online (Sandbox Code Playgroud) 我一直在学习STL在过去的两个星期,一直在处理很多的vector<T>,deque<T>和list<T>.我一直在使用所有这些时间push_back(),push_front(),insert().目前,我已经介绍了"插入迭代器",其中包括:
back_insert_iterator,类似于push_back()并且确实要求容器具有push_back()工作功能front_insert_iterator,类似于push_front()并要求容器具有push_front()insert_iterator,类似的insert(),等等等等等等所以我知道如何实现这一切.我的问题很简单,有什么区别?为什么要使用Insert Iterators呢?
问题说,我想在c ++中使用ifstream将类的自定义数据类型数据写入文件.需要帮忙.
c++ ×6
inserter ×6
iterator ×2
stl ×2
containers ×1
fstream ×1
iostream ×1
type-traits ×1
value-type ×1