array and vector

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

    #1

    array and vector

    Hi,

    I could use vector to get an array to random_shuffle it. Is it possible
    to define an array to random_shuffle it? I tried but it did not work.
    Can I use array[] instead of vector to use algorithm?

    Could you please help me out? Thanks.

    Michael

    1. vector (works)

    #include <iostream>
    #include <vector>
    #include <algorithm>

    using std::cout;

    void show_val(int x)
    {
    cout<<x<<endl;
    }

    int main()
    {

    vector<inta;

    for (int i=1; i<11;i++)
    a.push_back(i);

    random_shuffle( a.begin(), a.end());

    for_each(a.begi n(), a.end(), show_val);

    return 0;

    }

    2. array (not work)

    #include <iostream>
    #include <vector>
    #include <algorithm>

    using std::cout;

    void show_val(int x)
    {
    cout<<x<<endl;
    }


    int main()
    {

    int a [100];

    for (int i=0; i<100;i++)
    a[i]=i+1;

    random_shuffle( a.begin(), a.end());

    for_each(a.begi n(), a.end(), show_val);

    return 0;

    }

  • Alf P. Steinbach

    #2
    Re: array and vector

    * Michael:
    >
    int main()
    {
    >
    int a [100];
    >
    for (int i=0; i<100;i++)
    a[i]=i+1;
    >
    random_shuffle( a.begin(), a.end());
    A raw array does not have member functions.

    You could write

    random_shuffle( a, a+100);

    But raw arrays and pointers have numerous pitfalls for the novice (and
    for the experienced): in general it's not a good idea to use raw arrays,
    no matter how experienced one is, and given that you assumed there were
    member functions, in particular it's not a good idea for you to use raw
    arrays, except to learn about them.

    std::vector is much more safe.

    Additional suggestion, not related to the code above: instead of writing
    v[i], write v.at(i), which performs a range check and so is more safe.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Michael

      #3
      Re: array and vector


      Alf P. Steinbach wrote:
      * Michael:

      int main()
      {

      int a [100];

      for (int i=0; i<100;i++)
      a[i]=i+1;

      random_shuffle( a.begin(), a.end());
      >
      A raw array does not have member functions.
      >
      You could write
      >
      random_shuffle( a, a+100);
      >
      But raw arrays and pointers have numerous pitfalls for the novice (and
      for the experienced): in general it's not a good idea to use raw arrays,
      no matter how experienced one is, and given that you assumed there were
      member functions, in particular it's not a good idea for you to use raw
      arrays, except to learn about them.
      >
      std::vector is much more safe.
      >
      Additional suggestion, not related to the code above: instead of writing
      v[i], write v.at(i), which performs a range check and so is more safe.
      >
      --
      A: Because it messes up the order in which people normally read text.
      Q: Why is it such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?
      Thanks for your advice. I tried the following code by using at(). Why
      not working?

      #include <iostream>
      #include <vector>
      #include <algorithm>
      #include <cstring>

      using std::cout;


      int main(void)
      {

      vector<intv( 5, 1 );
      for( int i = 0; i < 10; i++ ) {
      cout << "Element " << i << " is " << v.at(i) << endl;
      }

      }

      Thanks in advance,
      Michael

      Comment

      • Alf P. Steinbach

        #4
        Re: array and vector

        * Michael:
        [Quoting signature, overquoting]
        Please don't quote signatures -- corrected.

        Also, please quote only the relevant parts you're replying to.


        * Michael:
        * Alf P. Steinbach:
        >Additional suggestion, not related to the code above: instead of writing
        >v[i], write v.at(i), which performs a range check and so is more safe.
        >
        Thanks for your advice. I tried the following code by using at(). Why
        not working?
        >
        #include <iostream>
        #include <vector>
        #include <algorithm>
        #include <cstring>
        >
        using std::cout;
        >
        >
        int main(void)
        {
        >
        vector<intv( 5, 1 );
        for( int i = 0; i < 10; i++ ) {
        cout << "Element " << i << " is " << v.at(i) << endl;
        }
        }
        When corrected so that it compiles (you need to write std::vector and
        std::endl) this program has Undefined Behavior. But, due to the use of
        std::vector::at instead of operator[] or a raw array, that UB can be
        easily converted to predictable behavior -- see below. Btw., the
        'void' argument list is C'ism that's better a-voided in C++. ;-)

        The program indexes the vector beyond its current size, using 'at'. The
        result of that is an exception. Here that exception results in
        Undefined Behavior (most probably a crash) because you don't catch the
        exception -- look up the 'try' and 'catch' keywords.

        With a raw array you would have Undefined Behavior with no means of
        turning it into something predictable & well defined.

        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • Michael

          #5
          Re: array and vector


          Alf P. Steinbach wrote:
          When corrected so that it compiles (you need to write std::vector and
          std::endl) this program has Undefined Behavior. But, due to the use of
          std::vector::at instead of operator[] or a raw array, that UB can be
          easily converted to predictable behavior -- see below. Btw., the
          'void' argument list is C'ism that's better a-voided in C++. ;-)
          >
          The program indexes the vector beyond its current size, using 'at'. The
          result of that is an exception. Here that exception results in
          Undefined Behavior (most probably a crash) because you don't catch the
          exception -- look up the 'try' and 'catch' keywords.
          >
          With a raw array you would have Undefined Behavior with no means of
          turning it into something predictable & well defined.
          >
          Thanks for your help! However, even if I change the upbound of i, it
          still did not work. Confused...

          #include <iostream>
          #include <vector>
          #include <algorithm>
          #include <cstring>

          using std::cout;

          int main(void)
          {

          vector<intv( 5, 1 );
          for( int i = 0; i < 10; i++ ) {
          cout << "Element " << i << " is " << v.at(i) << endl;
          }

          }

          Thanks,
          Michael

          Comment

          • Michael

            #6
            Re: array and vector


            Michael wrote:
            Thanks for your help! However, even if I change the upbound of i, it
            still did not work. Confused...
            >
            #include <iostream>
            #include <vector>
            #include <algorithm>
            #include <cstring>
            >
            using std::cout;
            >
            int main(void)
            {
            >
            vector<intv( 5, 1 );
            for( int i = 0; i < 3; i++ ) {
            cout << "Element " << i << " is " << v.at(i) << endl;
            }
            >
            }
            >
            Sorry about the typo, the upbound changed to 3, it did not work either.

            Michael

            Comment

            • Alf P. Steinbach

              #7
              Re: array and vector

              * Michael:
              * Michael:
              >
              >Thanks for your help! However, even if I change the upbound of i, it
              >still did not work. Confused...
              >>
              >#include <iostream>
              >#include <vector>
              >#include <algorithm>
              >#include <cstring>
              >>
              >using std::cout;
              >>
              >int main(void)
              >{
              >>
              > vector<intv( 5, 1 );
              > for( int i = 0; i < 3; i++ ) {
              > cout << "Element " << i << " is " << v.at(i) << endl;
              > }
              >>
              >}
              >>
              Sorry about the typo, the upbound changed to 3, it did not work either.
              1. See the FAQ item "How do I post a question about code that doesn't
              work correctly?", currently at <url:
              http://www.parashift.c om/c++-faq-lite/how-to-post.html#faq-5.8>. In
              particular, "did not work" says nothing about what you expected, and
              what happened or did not happen contrary to that expectation.

              2. Remove unnecessary headers, <algorithmand <cstring>.

              3. Add necessary header <ostream(althou gh your compiler probably does
              not require it).

              4. Remove the 'using' directive.

              5. Remove the 'void' C'ism.

              6. Add 'std::' before 'vector', 'cout' and 'endl'.

              7. See the FAQ item "Should I use using namespace std in my code?",
              currently at <url:
              http://www.parashift.c om/c++-faq-lite/coding-standards.html# faq-27.5>.

              --
              A: Because it messes up the order in which people normally read text.
              Q: Why is it such a bad thing?
              A: Top-posting.
              Q: What is the most annoying thing on usenet and in e-mail?

              Comment

              • Michael

                #8
                Re: array and vector


                Alf P. Steinbach wrote:
                >
                2. Remove unnecessary headers, <algorithmand <cstring>.
                >
                3. Add necessary header <ostream(althou gh your compiler probably does
                not require it).
                >
                4. Remove the 'using' directive.
                >
                5. Remove the 'void' C'ism.
                >
                6. Add 'std::' before 'vector', 'cout' and 'endl'.
                >
                7. See the FAQ item "Should I use using namespace std in my code?",
                currently at <url:
                http://www.parashift.c om/c++-faq-lite/coding-standards.html# faq-27.5>.
                Thanks for your advice!
                The error I got was:
                no matching function for call to `vector<int,all ocator<int::at (int
                &)'

                I did not see anything wrong with my code though. Could you please help
                me?

                Many thanks,
                Michael

                Comment

                • Salt_Peter

                  #9
                  Re: array and vector


                  Michael wrote:
                  Alf P. Steinbach wrote:

                  2. Remove unnecessary headers, <algorithmand <cstring>.

                  3. Add necessary header <ostream(althou gh your compiler probably does
                  not require it).

                  4. Remove the 'using' directive.

                  5. Remove the 'void' C'ism.

                  6. Add 'std::' before 'vector', 'cout' and 'endl'.

                  7. See the FAQ item "Should I use using namespace std in my code?",
                  currently at <url:
                  http://www.parashift.c om/c++-faq-lite/coding-standards.html# faq-27.5>.
                  >
                  Thanks for your advice!
                  The error I got was:
                  no matching function for call to `vector<int,all ocator<int::at (int
                  &)'
                  >
                  I did not see anything wrong with my code though. Could you please help
                  me?
                  >
                  Many thanks,
                  Michael
                  did you not even read the post above?
                  there is no such thing as a vector.

                  #include <iostream>
                  #include <ostream>
                  #include <vector>

                  int main()
                  {
                  const int sz(10);
                  std::vector<int v( sz, 1 );

                  for( int i = 0; i < sz; i++ )
                  {
                  std::cout << "v[ " << i << " ] = " << v.at(i);
                  std::cout << std::endl;
                  }
                  return 0;
                  }

                  ___
                  Now lets generate an exception and then catch it...
                  there is no 6th element in the vector.

                  #include <iostream>
                  #include <ostream>
                  #include <vector>
                  #include <stdexcept>

                  int main()
                  {
                  std::vector<int v( 5, 1 );

                  try
                  {
                  for( int i = 0; i < 6; i++ )
                  {
                  std::cout << "v[ " << i << " ] = " << v.at(i);
                  std::cout << std::endl;
                  }
                  }
                  catch ( const std::exception& e )
                  {
                  std::cout << "\n*** error: " << e.what();
                  std::cout << std::endl;
                  }
                  return 0;
                  }

                  /*

                  v[ 0 ] = 1
                  v[ 1 ] = 1
                  v[ 2 ] = 1
                  v[ 3 ] = 1
                  v[ 4 ] = 1

                  *** error: invalid vector<Tsubscri pt

                  */

                  Comment

                  Working...