处理载体< int *>作为向量< const int *>无需复制(C ++ 0x) [英] Treat vector<int*> as vector<const int*> without copying (C++0x)

查看:308
本文介绍了处理载体< int *>作为向量< const int *>无需复制(C ++ 0x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类包含 std :: vector< int *> 。外部代码需要对这个向量进行只读访问,不应该能够修改内容(既不是指针也不能修改它们的内容)。在类中,值可以改变(例如 double_values(),因此将它们存储为 std :: vector< ; const int *> 是不可能的。

A class contains a std::vector<int*>. External code needs read-only access to this vector, should not be able to modify the contents (neither the pointers or their contents). Inside the class, the values may change (e.g. double_values(), and so storing them as a std::vector<const int*> is not possible.

是否有办法返回 std :: vector < int *> 作为一个 std :: vector< const int *> 没有做一个副本感觉像应该有,因为const

Is there a way to return the std::vector<int*> as a std::vector<const int*> without making a copy? It feels like there should be, because const is simply operating at compile time to say what can and cannot be modified.

代码:(使用编译g ++ -std = c + + 0x

Code: (compile with g++ -std=c++0x)

class ReadOnlyAccess
{
public:
  ReadOnlyAccess(const std::vector<int*> & int_ptrs_param):
    int_ptrs(int_ptrs_param)
  {
  }
  const std::vector<int*> & get_int_ptrs() const
  {
    return int_ptrs;
  }
  std::vector<const int*> safely_get_int_ptrs() const
  {
    // will not compile (too bad):
    //    return int_ptrs;

    // need to copy entire vector
    std::vector<const int*> result(int_ptrs.size());
    for (int k=0; k<int_ptrs.size(); k++)
      result[k] = int_ptrs[k];
    return result;
  }
  void double_values()
  {
    for (int*p : int_ptrs)
      *p *= 2;
  }
  void print() const
  {
    for (const int * p : int_ptrs)
      std::cout << *p << " ";
    std::cout << std::endl;
  }
private:
  std::vector<int*> int_ptrs;
};

int main() {
  ReadOnlyAccess roa(std::vector<int*>{new int(10), new int(20), new int(100)});
  std::vector<const int*> safe_int_ptrs = roa.safely_get_int_ptrs();
  // does not compile (good)
  // *safe_int_ptrs[0] = -100000;
  roa.print();

  const std::vector<int*> & int_ptrs = roa.get_int_ptrs();
  // changes are made to the internal class values via the accessor! nooooo!
  *int_ptrs[0] = -100000;
  roa.print();

  return 0;
}


推荐答案

返回向量将意味着

但是,如果你的目标是提供一个方法来使用这些值而不修改它们,或修改它的容器,那么,基于访问者模式的算法可能是一个非常好的解决方案,特别是现在我们可以使用lambda表达式:

However, if your goal is to provide a way to use the values without modifying them, or modifying it's container, then a visitor pattern based algorithm might be a very good solution, in particular now that we can use lambda expressions:

#include <vector>
#include <iostream>

class Data
{
public:

    //...whatever needed to fill the values

    // here we assume that Func is equivalent to std::function< void ( int )> or std::function< void (const int& ) > and can return anything that will be ignored here.
    template< class Func > 
    void for_each_value( Func func ) const // read-only
    {
        for( const int* value : m_values ) // implicit conversion
        {
             func( *value ); // read-only reference (const &), or copy
             // if func needs to work with the adress of the object, it still can by getting a reference to it and using & to get it's adress
        }
    }


    void print() const
    {
        std::cout << "\nData values: \n";
        for_each_value( []( const int value ) { std::cout << "    "<< value << '\n'; } );
    }

    void count_values() const { return m_values.size(); }

private:

    std::vector<int*> m_values;

};



int main()
{
    Data data;
    // ... whatever needed to fill the data

    data.print();    

    std::vector<int> modified_values;
    data.for_each_value( [&]( int value ) { modified_values.push_back( value + 42 ); } );

    return 0;
}

如果您理解,并且可以减少使用值的不同方法到几个半通用算法,那么它将使你的代码更简单,并允许你将数据保存在你的结构,而不是暴露它的勇气。

If you understand that, and the different ways to use the values can be reduced to a few half-generic algorithms, then it will make your code simpler and allow you to keep data inside your structures instead of exposing it's the guts.

这篇关于处理载体&lt; int *&gt;作为向量&lt; const int *&gt;无需复制(C ++ 0x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆