C++ concatenate two int arrays into one larger array

前端 未结 6 2116
小蘑菇
小蘑菇 2020-12-08 16:20

Is there a way to take two int arrays in C++

int * arr1;
int * arr2;
//pretend that in the lines below, we fill these two arrays with different
//int values
         


        
6条回答
  •  时光取名叫无心
    2020-12-08 16:32

    Another alternative is to use expression templates and pretend the two are concatenated (lazy evaluation). Some links (you can do additional googling):

    http://www10.informatik.uni-erlangen.de/~pflaum/pflaum/ProSeminar/ http://www.altdevblogaday.com/2012/01/23/abusing-c-with-expression-templates/ http://aszt.inf.elte.hu/~gsd/halado_cpp/ch06s06.html

    If you are looking for ease of use, try:

    #include 
    #include 
    
    int main()
    {
      int arr1[] = {1, 2, 3};
      int arr2[] = {3, 4, 6};
    
      std::basic_string s1(arr1, 3);
      std::basic_string s2(arr2, 3);
    
      std::basic_string concat(s1 + s2);
    
      for (std::basic_string::const_iterator i(concat.begin());
        i != concat.end();
        ++i)
      {
        std::cout << *i << " ";
      }
    
      std::cout << std::endl;
    
      return 0;
    }
    

提交回复
热议问题