FrontPageRoast+>リファレンス>std>container_convert.hpp>container_convert

container_convert

コンテナ配列から、異なる型のコンテナ配列へと変換を行います。


C++宣言

roast/std/container_convert.hpp :

  1. namespace roast {
  2. template <typename container_from_T, typename container_to_T>
  3. void container_convert(container_from_T &from, container_to_T &to);
  4. }


パラメータ

  • from : コピー元コンテナ配列
  • to : コピー先コンテナ配列


解説

container_convert() 関数は、コンテナ配列 from から、異なるコンテナ配列 to へのコピーを行います。

from と to は同じコンテナ配列型である必要はありません。コンテナ配列型としては std::vector, std::list 等が使えますが、stdコンテナ配列以外の型を使用する事も出来ます。
ただしこの場合、以下の条件を満たしている必要があります。


  • from側が、std::vector, std::list と同等の、以下の機能を満たしている事。
    • iterator をサポートしている事。
      • operator !=() をサポートしている事
      • operator ++() をサポートしている事
    • begin() をサポートしている事。
    • end() をサポートしてる事。
  • to側が、std::vector, std::list と同等の、以下の機能を満たしている事。
    • push_back() をサポートしている事。


from と to の要素の型は異なっていても構いません。ただしこの場合、to の要素型は、from の要素型からの変換をサポートしている必要があります。
(to の要素型がクラスである場合、from の要素型を引数とするコンストラクタを実装している必要があります)

以下に示すサンプルコードでは、from と to のコンテナ型も、要素の型も異なっている場合を例に取り上げます。


使用例 (サンプルコード)

  1. class CHoge1{
  2. public:
  3. int m_a;
  4. CHoge1(int x){ m_a = x; }
  5. };
  6. class CHoge2{
  7. public:
  8. int m_b;
  9. CHoge2(const CHoge1 &from){
  10. m_b = from.m_a;
  11. }
  12. };
  13. void main()
  14. {
  15. std::list<CHoge1> hoge1Ary;
  16. std::vector<CHoge2> hoge2Ary;
  17. hoge1Ary.push_back(CHoge1(10));
  18. hoge1Ary.push_back(CHoge1(20));
  19. container_convert(hoge1Ary, hoge2Ary);
  20. printf("%d\n", hoge2Ary[0].m_b );
  21. printf("%d\n", hoge2Ary[1].m_b );
  22. }


サポート版数

初期版数からサポート。