11

我有一个算法,它需要一个std::vector(调用它A)。但是,我已经有了B条目N + 2,我基本上想要的是通过B.data() + 2,所以算法NB. 如果A被修改,那么也是B

当使用double*指针时,我应该如何制作它是非常清楚的,但是这也可以用std::vectors 吗?我的意思是,向量的好处是它为我处理内存,而我现在想要的是禁止它(如果B或被A破坏,它们应该保持指向的数据不变)。

像这样:

std::vector< double > B({1,2,3,4,5});
std::vector< double > A(B.data() + 2, B.size() - 2);

// A and B share data now. A is 3, 4, 5

我知道通过采用一对迭代器可以更好地为此目的设计算法,但这不在我手中。

更新

(在评论中,希望看到签名,这里是)

nlopt::result nlopt::opt::optimize(std::vector<double> &x, double &opt_f);

但是,我的初衷是非常聪明,让算法直接优化我的 vector B,所以我最终得到的是这样的:

std::vector< double > B(N + 2);
// do something with it here, like put initial values

std::vector< double > A( B.begin() + 2, B.end() );
optimize( A );

std::copy(A.begin(), A.end(), B.begin() + 2);

我真的不担心这种解决方法,我还在文档中读到,nlopt如果使用 C++ 接口而不是 C 接口,则向量在内部无论如何都是复制的。

但同样,这个例子确实让我对算法界面设计大开眼界,值得花一些时间浏览诸如 boost 库之类range的东西。

4

3 回答 3

7

实际上,我在一个项目中遇到了类似的问题,这是我想出的解决方案:

#include <iterator>
#include <type_traits>

/** \brief Provides an interface to random accessible ranges
  * \tparam RAIterator must be a random access iterator
  *
  * This class doesn't allocate any memory by itself and
  * provides only an interface into the data of another
  * container.
  * \attention Keep in mind that although all methods are
  * \c const, the returned RAIterator might not be a \c const_iterator
  * at all. It is your responsibility to make sure that
  * you don't invalidate the given range while working on it.
**/

template <class RAIterator>
class Slice
{
public:
    //! Typedef for convenience when working with standard algorithms
    typedef RAIterator iterator;

    //! Alias to the iterator's reference type
    typedef typename std::iterator_traits<RAIterator>::reference reference;

    //! Creates the slice.
    //! \param first, last a valid range
    Slice(RAIterator first, RAIterator last) : first(first), last(last){}

    //! Creates the slice.
    //! \param first iterator to the first element
    //! \param length of the range [first, first+length)
    //! \remark if length is negative, an empty slice is assumed.
    Slice(RAIterator first, int length) : 
        first(first), 
        last( length > 0 ? first + length : first)
    {}

    //! The default constructor is deleted, as it would resemble an empty slice
    Slice() = delete;

    ///@{
    //! \brief Defaulted construcors.
    Slice(const Slice&) = default;
    Slice(Slice&&) = default;
    Slice& operator=(const Slice&)= default;
    Slice& operator=(Slice&&)= default;
    /**@}*/

    //! Returns an iterator to the begin of the range
    RAIterator begin() const{ return first; }

    //! Returns an iterator to the end of the range
    RAIterator end()   const{ return last;  }

    //! Returns the size of the slice.
    typename std::iterator_traits<RAIterator>::difference_type size() const{
        return std::distance(first, last);
    }

    //! Provides random access for the values interfaced by Slice
    reference operator[](size_t index) const { return first[index]; }

private:
    RAIterator first; //!< begin of the range
    RAIterator last;  //!< end of the range
};

/** \brief Creates a slice from the given range
  * \tparam RAIterator should be an random access iterator
  * \returns a slice [first,last)
  * \param first, last is the range
**/
template <class RAIterator>
Slice<RAIterator> make_slice(RAIterator first, RAIterator last){
    return Slice<RAIterator>(first, last);
}

现在您可以Slice像在您的示例中一样使用:

std::vector< double > B({1,2,3,4,5});
Slice A(B.begin() + 2, B.size() - 2);
A[0] = 5;
// B == {1,2,5,4,5}

编辑:如果您想要更成熟的切片,boost::adaptors::slice请尽可能使用。

于 2013-10-23T08:26:18.713 回答
0

Just crazy answer.

I've thought about using vector of shared_ptr<double>, where each shared_ptr points to individual double. Thus it looks ugly and might be painfull, those vectors will share the memory that contains doubles.

于 2013-10-23T08:35:55.880 回答
0

只是两个想法。

  1. 如果函数引用 std::vector - 尝试使用 std::vector 作为基类创建您的类。

  2. 尝试使用分配器,它是 std::vector 的第二个模板参数。有了它,您可以根据需要分配或仅使用已分配的内存。 http://www.cplusplus.com/reference/vector/vector/ http://www.cplusplus.com/reference/vector/vector/get_allocator/

于 2013-10-23T08:29:09.463 回答