array and vector

M

Michael

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;

}
 
A

Alf P. Steinbach

* 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.
 
M

Michael

Alf said:
* 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
 
A

Alf P. Steinbach

* 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.
 
M

Michael

Alf said:
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
 
M

Michael

Michael said:
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
 
A

Alf P. Steinbach

* Michael:
* Michael:

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>.
 
M

Michael

Alf said:
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
 
S

Salt_Peter

Michael said:
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

*/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top