array and vector

Discussion in 'C++' started by Michael, Aug 12, 2006.

  1. Michael

    Michael Guest

    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<int> a;

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

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

    for_each(a.begin(), 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+1;

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

    for_each(a.begin(), a.end(), show_val);

    return 0;

    }
    Michael, Aug 12, 2006
    #1
    1. Advertising

  2. * Michael:
    >
    > int main()
    > {
    >
    > int a [100];
    >
    > for (int i=0; i<100;i++)
    > a=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, 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?
    Alf P. Steinbach, Aug 12, 2006
    #2
    1. Advertising

  3. Michael

    Michael Guest

    Alf P. Steinbach wrote:
    > * Michael:
    > >
    > > int main()
    > > {
    > >
    > > int a [100];
    > >
    > > for (int i=0; i<100;i++)
    > > a=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, 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<int> v( 5, 1 );
    for( int i = 0; i < 10; i++ ) {
    cout << "Element " << i << " is " << v.at(i) << endl;
    }

    }

    Thanks in advance,
    Michael
    Michael, Aug 12, 2006
    #3
  4. * 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, 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<int> v( 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?
    Alf P. Steinbach, Aug 12, 2006
    #4
  5. Michael

    Michael Guest

    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<int> v( 5, 1 );
    for( int i = 0; i < 10; i++ ) {
    cout << "Element " << i << " is " << v.at(i) << endl;
    }

    }

    Thanks,
    Michael
    Michael, Aug 12, 2006
    #5
  6. Michael

    Michael Guest

    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<int> v( 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
    Michael, Aug 12, 2006
    #6
  7. * 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<int> v( 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.com/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, <algorithm> and <cstring>.

    3. Add necessary header <ostream> (although 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.com/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?
    Alf P. Steinbach, Aug 12, 2006
    #7
  8. Michael

    Michael Guest

    Alf P. Steinbach wrote:
    >
    > 2. Remove unnecessary headers, <algorithm> and <cstring>.
    >
    > 3. Add necessary header <ostream> (although 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.com/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,allocator<int> >::at (int
    &)'

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

    Many thanks,
    Michael
    Michael, Aug 12, 2006
    #8
  9. Michael

    Salt_Peter Guest

    Michael wrote:
    > Alf P. Steinbach wrote:
    > >
    > > 2. Remove unnecessary headers, <algorithm> and <cstring>.
    > >
    > > 3. Add necessary header <ostream> (although 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.com/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,allocator<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<T> subscript

    */
    Salt_Peter, Aug 12, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,698
  2. Replies:
    8
    Views:
    1,873
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    532
    James Kanze
    Sep 4, 2007
  4. Thomas
    Replies:
    7
    Views:
    293
    Mark Hubbart
    May 23, 2005
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    338
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page