我有一个黑盒 C++ 函数,我无法访问它的源代码:
void blackbox(vector<int> &input);
此函数以未知方式修改输入 vector 的元素。
我现在遇到的问题是我只想对 vector 的部分片段应用黑盒功能,例如, vector 的最后 500 个元素。因此,这是我为实现此目标而编写的例程:
vector<int> foo (5,1000);
vector<int> bar (foo.end()-500,foo.end());
blackbox(bar);
swap_ranges(foo.end()-500,foo.end(),bar.begin());
此代码可能有效,但有更好的方法吗?
如果我可以只为一段定义一个 vector 引用就好了 现有 vector ,而不是创建拷贝。 我对上面代码中的 copy-and-swap 部分不太满意;因为这个例程是 如此频繁地调用,我认为重复的 copy-and-swap 会减慢代码速度。 如果我知道 block 框完成的确切操作,我会重写函数,使其将 vector 迭代器作为输入 争论。不幸的是,目前这是不可能的。
最佳答案
没有明确定义的方法来实现此功能。带有大量的警告和警告,它可以(至少对于一个 GCC 版本)被如下破解,或者您可以编写一些具有更好定义的行为但基于编译器当前的 std::vector
实现的东西。 ...
所以...被黑了。如果 insert
/erase
/resize
/reserve
/clear
/这将不起作用push_back
或任何其他影响整个 vector 的操作被执行。它可能无法移植/继续工作/使用所有优化级别/在星期二工作/使用风险自负等。这取决于空基类优化。
你需要一个自定义分配器,但有一个问题:分配器不能有任何状态,否则它会改变 vector
对象的二进制布局,所以我们最终得到这个:
#include <iostream>
#include <vector>
template <typename Container> // easy to get this working...
void f(Container& v)
{
std::cout << "f() v.data() " << v.data() << ", v.size() " << v.size() << '\n';
for (int& n : v) n += 10;
}
void g(std::vector<int>& v) // hard to get this working...
{
std::cout << "g() v.data() " << v.data() << ", v.size() " << v.size() << '\n';
for (int& n : v) n += 100;
}
int* p_; // ouch: can't be a member without changing vector<> memory layout
struct My_alloc : std::allocator<int>
{
// all no-ops except allocate() which returns the constructor argument...
My_alloc(int* p) { p_ = p; }
template <class U, class... Args>
void construct(U* p, Args&&... args) { std::cout << "My_alloc::construct(U* " << p << ")\n"; }
template <class U> void destroy(U* p) { std::cout << "My_alloc::destroy(U* " << p << ")\n"; }
pointer allocate(size_type n, std::allocator<void>::const_pointer hint = 0)
{
std::cout << "My_alloc::allocate() return " << p_ << "\n";
return p_;
}
void deallocate(pointer p, size_type n) { std::cout << "deallocate\n"; }
template <typename U>
struct rebind { typedef My_alloc other; };
};
int main()
{
std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std::cout << "main() v.data() " << v.data() << '\n';
My_alloc my_alloc(&v[3]); // first element to "take over"
std::vector<int, My_alloc> w(3, my_alloc); // num elements to "take over"
f(w);
g(reinterpret_cast<std::vector<int>&>(w));
for (int n : v) std::cout << n << ' ';
std::cout << '\n';
std::cout << "sizeof v " << sizeof v << ", sizeof w " << sizeof w << '\n';
}
输出:
main() v.data() 0x9d76008
My_alloc::allocate() return 0x9d76014
My_alloc::construct(U* 0x9d76014)
My_alloc::construct(U* 0x9d76018)
My_alloc::construct(U* 0x9d7601c)
f() v.data() 0x9d76014, v.size() 3
g() v.data() 0x9d76014, v.size() 3
0 1 2 113 114 115 6 7 8 9
sizeof v 12, sizeof w 12
My_alloc::destroy(U* 0x9d76014)
My_alloc::destroy(U* 0x9d76018)
My_alloc::destroy(U* 0x9d7601c)
deallocate
看它运行here
关于c++ - 引用 vector 的部分片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28621686/