Efficiently creating a vector<char> which is a copy of a char[]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Reddie

    Efficiently creating a vector<char> which is a copy of a char[]

    I have a large char[] which I'd like to copy a portion of into a
    vector<char>. I can't seem to locate a simple method for doing so. I
    assume involving iterators will not result in memcpy not being used
    and therefore will be less than optimal. Can anyone help me out?

    Regards,

    Steven
  • Alf P. Steinbach

    #2
    Re: Efficiently creating a vector&lt;char& gt; which is a copy of a char[]

    On 5 Feb 2004 21:28:39 -0800, smr@essemer.com .au (Steven Reddie) wrote:
    [color=blue]
    >I have a large char[] which I'd like to copy a portion of into a
    >vector<char> . I can't seem to locate a simple method for doing so. I
    >assume involving iterators will not result in memcpy not being used
    >and therefore will be less than optimal. Can anyone help me out?[/color]

    Check the std::vector constructors for constructing a vector from a
    character array.

    Check the resizing member functions and std::copy for copying into
    an existing vector.

    Comment

    • Jonathan Turkanis

      #3
      Re: Efficiently creating a vector&lt;char& gt; which is a copy of a char[]

      "Steven Reddie" <smr@essemer.co m.au> wrote in message
      news:f93791bd.0 402052128.5582a 02a@posting.goo gle.com...[color=blue]
      > I have a large char[] which I'd like to copy a portion of into a
      > vector<char>. I can't seem to locate a simple method for doing so.[/color]
      I[color=blue]
      > assume involving iterators will not result in memcpy not being used
      > and therefore will be less than optimal. Can anyone help me out?
      >[/color]

      A good implementation of std::copy will use memcpy, memmove or
      something similar in the case you describe. You could also use
      char_traits<cha r>::copy.

      Jonathan


      Comment

      • tom_usenet

        #4
        Re: Efficiently creating a vector&lt;char& gt; which is a copy of a char[]

        On 5 Feb 2004 21:28:39 -0800, smr@essemer.com .au (Steven Reddie)
        wrote:
        [color=blue]
        >I have a large char[] which I'd like to copy a portion of into a
        >vector<char> . I can't seem to locate a simple method for doing so. I
        >assume involving iterators will not result in memcpy not being used
        >and therefore will be less than optimal. Can anyone help me out?[/color]

        std::vector<cha r> v(chararray, chararray + sizeof(chararra y));

        will collapse to a memcpy or memmove call on most (all?) vector
        implementations I know of. Trace in with a debugger to confirm.

        Tom

        C++ FAQ: http://www.parashift.com/c++-faq-lite/
        C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

        Comment

        • Steven Reddie

          #5
          Re: Efficiently creating a vector&lt;char& gt; which is a copy of a char[]

          tom_usenet <tom_usenet@hot mail.com> wrote in message news:<gku620hmh p57gqc1m88kt4ov b40h2lsvr8@4ax. com>...[color=blue]
          > On 5 Feb 2004 21:28:39 -0800, smr@essemer.com .au (Steven Reddie)
          > wrote:
          >
          > std::vector<cha r> v(chararray, chararray + sizeof(chararra y));
          >
          > will collapse to a memcpy or memmove call on most (all?) vector
          > implementations I know of. Trace in with a debugger to confirm.[/color]

          Thanks guys. It looks like I'm misreading the docs. This last form
          of constructor must be the one defined as:

          template<class InputIterator> vector(InputIte rator _First,
          InputIterator _Last);

          which implies that InputIterator for a vector<char> is or is
          implicitly constructed from a char*. It's been a while since I've
          used the STL that it seems that I'm forgetting some really basic
          stuff.

          Regards,

          Steven

          Comment

          • tom_usenet

            #6
            Re: Efficiently creating a vector&lt;char& gt; which is a copy of a char[]

            On 6 Feb 2004 19:14:52 -0800, smr@essemer.com .au (Steven Reddie)
            wrote:
            [color=blue]
            >tom_usenet <tom_usenet@hot mail.com> wrote in message news:<gku620hmh p57gqc1m88kt4ov b40h2lsvr8@4ax. com>...[color=green]
            >> On 5 Feb 2004 21:28:39 -0800, smr@essemer.com .au (Steven Reddie)
            >> wrote:
            >>
            >> std::vector<cha r> v(chararray, chararray + sizeof(chararra y));
            >>
            >> will collapse to a memcpy or memmove call on most (all?) vector
            >> implementations I know of. Trace in with a debugger to confirm.[/color]
            >
            >Thanks guys. It looks like I'm misreading the docs. This last form
            >of constructor must be the one defined as:
            >
            >template<cla ss InputIterator> vector(InputIte rator _First,
            >InputIterato r _Last);
            >
            >which implies that InputIterator for a vector<char> is or is
            >implicitly constructed from a char*.[/color]

            That constructor is the one that matches. You end up calling:

            vector<char>::v ector<char*>(ch ar* _First, char* _Last);

            That templated constructor should dispatch to a memmove/memcpy
            implementation, since char is a scalar and char* is a simple pointer
            or the same type.

            Tom

            C++ FAQ: http://www.parashift.com/c++-faq-lite/
            C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

            Comment

            Working...