Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. array => vector

array => vector

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdata-structures
11 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V VeganFanatic

    what do I need as an overload= for the array type I have a vector template that needs an overload

    http://www.contract-developer.tk

    S Offline
    S Offline
    Stephen Hewitt
    wrote on last edited by
    #2

    Perhaps this well set you on the right path:

    // Console.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <tchar.h>
    #include <iostream>

    using namespace std;

    class MyVector
    {
    public:
    template <typename T, size_t N>
    MyVector& operator=(const T(&a)[N])
    {
    for (size_t i=0; i<N; ++i)
    {
    cout << a[i] << " ";
    }

    	cout << endl;
    
    	return \*this;
    }
    

    };

    int _tmain(int argc, _TCHAR* argv[])
    {
    MyVector vec;
    int n[] = {1, 2, 3, 4};
    const char* s[] = {"One", "Two", "Three", "Four"};
    vec = n;
    vec = s;

    cout << endl;
    

    }

    Output:

    1 2 3 4
    One Two Three Four

    NOTE: Visual Studio 6 can't handle it.

    Steve

    V 1 Reply Last reply
    0
    • S Stephen Hewitt

      Perhaps this well set you on the right path:

      // Console.cpp : Defines the entry point for the console application.
      //

      #include "stdafx.h"
      #include <tchar.h>
      #include <iostream>

      using namespace std;

      class MyVector
      {
      public:
      template <typename T, size_t N>
      MyVector& operator=(const T(&a)[N])
      {
      for (size_t i=0; i<N; ++i)
      {
      cout << a[i] << " ";
      }

      	cout << endl;
      
      	return \*this;
      }
      

      };

      int _tmain(int argc, _TCHAR* argv[])
      {
      MyVector vec;
      int n[] = {1, 2, 3, 4};
      const char* s[] = {"One", "Two", "Three", "Four"};
      vec = n;
      vec = s;

      cout << endl;
      

      }

      Output:

      1 2 3 4
      One Two Three Four

      NOTE: Visual Studio 6 can't handle it.

      Steve

      V Offline
      V Offline
      VeganFanatic
      wrote on last edited by
      #3

      For some reason VS 2010 does not like that. Do I need more assignment overloading? vector operator= (vector &that) { // assignment operator for (int i=0; i < that.data.size(); i++) this->data[i] = that.data[i]; return *this; } vector operator= (base* that) { // assignment operator for (int i=0; i < sizeof(that); i++) this->data[i] = that[i]; return *this; }

      http://www.contract-developer.tk

      S 1 Reply Last reply
      0
      • V VeganFanatic

        For some reason VS 2010 does not like that. Do I need more assignment overloading? vector operator= (vector &that) { // assignment operator for (int i=0; i < that.data.size(); i++) this->data[i] = that.data[i]; return *this; } vector operator= (base* that) { // assignment operator for (int i=0; i < sizeof(that); i++) this->data[i] = that[i]; return *this; }

        http://www.contract-developer.tk

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #4

        It looks like you need something like this instead of the second overload:

        template <std::size_t N>
        vector& operator=(const base(&that)[N])
        {
        for (int i=0; i<N; ++i)
        data[i] = that[i];
        return *this;
        }

        sizeof(that) is wrong for may reasons. It's the size of a single base type in bytes. Also, read up on array to pointer decay. NOTE: Both overload should return vector&, not vector. PS: No animals where harmed while posting this code.

        Steve

        V 1 Reply Last reply
        0
        • S Stephen Hewitt

          It looks like you need something like this instead of the second overload:

          template <std::size_t N>
          vector& operator=(const base(&that)[N])
          {
          for (int i=0; i<N; ++i)
          data[i] = that[i];
          return *this;
          }

          sizeof(that) is wrong for may reasons. It's the size of a single base type in bytes. Also, read up on array to pointer decay. NOTE: Both overload should return vector&, not vector. PS: No animals where harmed while posting this code.

          Steve

          V Offline
          V Offline
          VeganFanatic
          wrote on last edited by
          #5

          This variable N, is that second needed? My goal... matrix a; matrix[0] = {0, 1, 4, 1, 9}; matrix[1] = {....}; and so on, so I am trying to get the vector base template working better

          http://www.contract-developer.tk

          modified on Thursday, June 17, 2010 8:38 PM

          S 1 Reply Last reply
          0
          • V VeganFanatic

            This variable N, is that second needed? My goal... matrix a; matrix[0] = {0, 1, 4, 1, 9}; matrix[1] = {....}; and so on, so I am trying to get the vector base template working better

            http://www.contract-developer.tk

            modified on Thursday, June 17, 2010 8:38 PM

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #6

            I don't think this kind of thing can be done unless you've got some brand spanking new C++0x[^] compliant compiler. Specifically the C++0x "Initializer lists" feature.

            Steve

            V 1 Reply Last reply
            0
            • S Stephen Hewitt

              I don't think this kind of thing can be done unless you've got some brand spanking new C++0x[^] compliant compiler. Specifically the C++0x "Initializer lists" feature.

              Steve

              V Offline
              V Offline
              VeganFanatic
              wrote on last edited by
              #7

              Guess I am waiting for Visual Studio 2010 SP1

              http://www.contract-developer.tk

              S 1 Reply Last reply
              0
              • V VeganFanatic

                Guess I am waiting for Visual Studio 2010 SP1

                http://www.contract-developer.tk

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #8

                Have a look at Boost[^]'s Boost.Assign[^] library.

                Steve

                V 1 Reply Last reply
                0
                • S Stephen Hewitt

                  Have a look at Boost[^]'s Boost.Assign[^] library.

                  Steve

                  V Offline
                  V Offline
                  VeganFanatic
                  wrote on last edited by
                  #9

                  I have looked the std::array and I will see what I can do about casting that to my vector template. #include

                  http://www.contract-developer.tk

                  V 1 Reply Last reply
                  0
                  • V VeganFanatic

                    I have looked the std::array and I will see what I can do about casting that to my vector template. #include

                    http://www.contract-developer.tk

                    V Offline
                    V Offline
                    VeganFanatic
                    wrote on last edited by
                    #10

                    That was another waste of time. Sure seems VC 2010 is hostile to data processing.

                    http://www.contract-developer.tk

                    1 Reply Last reply
                    0
                    • V VeganFanatic

                      what do I need as an overload= for the array type I have a vector template that needs an overload

                      http://www.contract-developer.tk

                      A Offline
                      A Offline
                      Aescleal
                      wrote on last edited by
                      #11

                      If you want to do something with an array and not a pointer to a lump of data then declare the function as:

                      template <std::size_t N> my_class &operator=(T (&t)[N])

                      where T is the type you're templating on. Usually what you do is have a copy constructor that takes the same parameters and then you implement operator= in terms of it (which can improve exception saftety as well if you do it right):

                      template <std::size_t N> my_class &operator=( T (&t)[N] )
                      {
                      my_class temp( t );
                      std::swap( temp. *this );
                      return *this;
                      }

                      The important thing is getting the size of the array into the function. Another way of doing this, which is common in the STL, is to specify a range of interators, in this case you could use a range of T pointers:

                      my_class &operator=( const T *begin, const T * const end )

                      I'd strongly recommend you grab a copy of "Exceptional C++" by Herb Sutter - it's got loads of guidance on how to write assignment operators. Cheers, Ash

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups