从类返回引用向量
Returning a vector of references from a Class
我有一个名为FunctionCombiner的函数(基本类型ValuationFunction(,其中包含其他估值函数,如下所示:
class FunctionCombiner : public valuationFunction
{
public:
FunctionCombiner(std::vector<std::shared_ptr<valuationFunction>>& Inner_);
void ValueInstrument();
std::vector<std::string> GetuniqueIdentifier() const;
void RiskFactorAdd(double increment, RiskFactor simulatedRiskFactor);
void RiskFactorMultiply(double factor, RiskFactor simulatedRiskFactor);
virtual valuationFunction* clone() const;
private:
std::vector<std::shared_ptr<valuationFunction>> Inner;
};
我需要做的是创建一个函数来返回对这些"内部"函数的引用,但我不确定该怎么做,我尝试过的所有内容似乎在语法上都失败了。如果我只返回一个引用(不在向量内(,我会像这样做:
valuationFunction& FunctionCombiner ::GetInner()
{
return *this;
}
我尝试返回类似std::vector< valuationFunction&>
的东西,但编译器似乎不太喜欢这样。正确的方法是什么?
最终目标是收集所有这些对内部函数的引用(来自多个 FunctionCombiner 或不同的评估函数(,以便比较它们并在以后整理重复项。
编辑:从答案中,我现在实现了返回内部引用的函数,如下所示: 对于"内部"类,我的函数现在看起来像这样:
std::vector<std::reference_wrapper<valuationFunction>> StockPriceFunction::GetInnerReference()
{
std::vector<std::reference_wrapper<valuationFunction>> innerVector(1);
innerVector.push_back(std::ref(*this));
return innerVector;
}
就像组合器一样:
std::vector<std::reference_wrapper<valuationFunction>> FunctionCombiner::GetInnerReference()
{
std::vector<std::reference_wrapper<valuationFunction>> innerVector(Inner.size());
for (unsigned long i = 0; i < Inner.size(); ++i) {
std::vector<std::reference_wrapper<valuationFunction>> innerInnerVector = Inner[i]->GetInnerReference();
innerVector.insert(innerVector.end(), innerInnerVector.begin(), innerInnerVector.end());
}
return innerVector;
}
但它不是编译并给我奇怪的错误,我在这里做错了什么?
为了在std::vector
中存储引用,您必须使用 std::reference_wrapper。
#include <functional>
#include <vector>
int main( )
{
std::vector<int> ints{ 1, 2, 3, 4, 5 };
std::vector<std::reference_wrapper<int>> references;
for( auto& i : ints )
references.push_back( i );
auto& ref{ references[ 1 ].get( ) };
ref += 20;
// Prints 22.
std::cout << ints[ 1 ] << 'n';
}
这是另一个示例,它更接近您正在尝试执行的操作(我还包括了您需要的标头(。
#include <iostream>
#include <functional>
#include <vector>
#include <memory>
struct ValuationFunction
{
ValuationFunction( int prop )
: property{ prop } { }
int property;
};
// Converts the vector of pointers to a vector
// of references.
static std::vector<std::reference_wrapper<ValuationFunction>>
ToReference( const std::vector<std::shared_ptr<ValuationFunction>>& pointers )
{
std::vector<std::reference_wrapper<ValuationFunction>> references;
references.reserve( pointers.size( ) );
for ( auto& pointer : pointers )
references.emplace_back( *pointer );
return references;
}
int main( )
{
std::vector<std::shared_ptr<ValuationFunction>> pointers
{
std::make_shared<ValuationFunction>( 1 ),
std::make_shared<ValuationFunction>( 2 ),
std::make_shared<ValuationFunction>( 3 ),
std::make_shared<ValuationFunction>( 4 )
};
auto references{ ToReference( pointers ) };
// Use 'std::reference_wrapper.get( )' to get
// access to the contained reference.
for ( auto& ref : references )
std::cout << "Before: " << ref.get( ).property << 'n';
// Change the property value of each 'ValuationFunction'
// in the original list.
for ( auto& pointer : pointers )
pointer->property += 10;
// Re-print the property value of each reference and see
// how they have all increased by 10.
for ( auto& ref : references )
std::cout << "After: " << ref.get( ).property << 'n';
}
相关文章:
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 创建引用向量的优雅方式
- C++重新引用向量中的值
- 很好的语法来获取对向量/数组数据的大小引用?
- 如何返回向量的常量引用?
- 取消引用向量时出现问题
- 为什么通过 vector<reference_wrapper> 的元素删除引用的值<T>不会使向量无效?
- 通过引用传递向量是请求 std::分配器
- 从类返回引用向量
- 返回两个向量 – 使用引用还是元组?
- 在C++中通过引用传递向量数组
- 引用向量中的特定索引
- 如何告诉自动推断向量<bool>元素的非引用类型
- 如何在方法主体中返回声明向量的引用?
- 向量引用C++
- 在向量引用上调用 vector::reserve()
- 如何创建一个同时接受数组和向量引用的函数模板
- 如何做列表的向量引用列表中的值
- c++向量引用转换
- 最方便的表示向量引用的向量的方法