Again a simple question about std::vector

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

    Again a simple question about std::vector

    Hi,

    Again I am wondering about something.

    If you use the swap function of an std::vector, what does it do?

    Does it swap each element separately by means of copying to a tmp
    variable, or does it just swap some pointers to memory blocks.

    Regards
  • Victor Bazarov

    #2
    Re: Again a simple question about std::vector

    ciccio wrote:
    Again I am wondering about something.
    >
    If you use the swap function of an std::vector, what does it do?
    >
    Does it swap each element separately by means of copying to a tmp
    variable, or does it just swap some pointers to memory blocks.
    In most implementations , the latter. Of course it also has to
    swap the size if it's cached (and it probably is), and other data
    members.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Jerry Coffin

      #3
      Re: Again a simple question about std::vector

      In article <ft2ck5$2j4$1@i karia.belnet.be >, no_valid_email@ spam.com
      says...
      Hi,
      >
      Again I am wondering about something.
      >
      If you use the swap function of an std::vector, what does it do?
      >
      Does it swap each element separately by means of copying to a tmp
      variable, or does it just swap some pointers to memory blocks.
      It's pretty much required to do the latter. It's required to have
      constant complexity, and it's can't throw an exception (a swap() can
      only throw an exception if the container's Compare() function throws the
      exception, and a vector doesn't have a Compare() function).

      Contrary to the implication elsethread, there's no real difference
      between std::swap<vecto r, vectorand vector::swap(ve ctor). According to
      section 23.2.4.4, the standard library is required to contain a
      specialization of std::swap for vector so that if v1 and v2 both refer
      to vectors, std:swap(v1, v2) is equivalent to v1.swap(v2).

      --
      Later,
      Jerry.

      The universe is a figment of its own imagination.

      Comment

      • Lionel B

        #4
        Re: Again a simple question about std::vector

        On Thu, 03 Apr 2008 08:14:18 -0600, Jerry Coffin wrote:
        In article <ft2ck5$2j4$1@i karia.belnet.be >, no_valid_email@ spam.com
        says...
        >Hi,
        >>
        >Again I am wondering about something.
        >>
        >If you use the swap function of an std::vector, what does it do?
        >>
        >Does it swap each element separately by means of copying to a tmp
        >variable, or does it just swap some pointers to memory blocks.
        [...]
        Contrary to the implication elsethread, there's no real difference
        between std::swap<vecto r, vectorand vector::swap(ve ctor). According to
        section 23.2.4.4, the standard library is required to contain a
        specialization of std::swap for vector so that if v1 and v2 both refer
        to vectors, std:swap(v1, v2) is equivalent to v1.swap(v2).
        Right, I wasn't aware that it was actually a requirement. I guess that
        applies to every container, then (I can't think why it shouldn't).

        --
        Lionel B

        Comment

        • James Kanze

          #5
          Re: Again a simple question about std::vector

          On 3 avr, 16:14, Jerry Coffin <jcof...@taeus. comwrote:
          In article <ft2ck5$2j...@i karia.belnet.be >, no_valid_em...@ spam.com
          says...
          If you use the swap function of an std::vector, what does it do?
          Does it swap each element separately by means of copying to a tmp
          variable, or does it just swap some pointers to memory blocks.
          It's pretty much required to do the latter. It's required to have
          constant complexity, and it's can't throw an exception (a swap() can
          only throw an exception if the container's Compare() function throws the
          exception, and a vector doesn't have a Compare() function).
          I've been wondering about this. What about the allocators? If
          I understand the philosophy behind them, all memory that was
          allocated must be freed by an allocator of the same type, which
          compares equal to the one used to allocate. The same type is
          automatic, since the type of the allocator is part of the type
          of the template specialization. But maintaining the second
          condition means somehow swapping the allocators as well, at
          least if they don't compare equal. And off hand, I don't see
          any requirement that allocators are "Swappable" , nor that they
          cannot throw if you attempt to swap them with std::swap.

          Is this an oversight in the standard, or is there something I've
          missed?

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • Jerry Coffin

            #6
            Re: Again a simple question about std::vector

            In article <ft2qiv$d07$2@s outh.jnrs.ja.ne t>, me@privacy.net says...

            [ ... ]
            Contrary to the implication elsethread, there's no real difference
            between std::swap<vecto r, vectorand vector::swap(ve ctor). According to
            section 23.2.4.4, the standard library is required to contain a
            specialization of std::swap for vector so that if v1 and v2 both refer
            to vectors, std:swap(v1, v2) is equivalent to v1.swap(v2).
            >
            Right, I wasn't aware that it was actually a requirement. I guess that
            applies to every container, then (I can't think why it shouldn't).
            I believe that's correct, yes.

            --
            Later,
            Jerry.

            The universe is a figment of its own imagination.

            Comment

            Working...