473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.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]=i+1;

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

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

return 0;

}

Aug 12 '06 #1
8 1910
* 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?
Aug 12 '06 #2

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

Aug 12 '06 #3
* 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?
Aug 12 '06 #4

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

Aug 12 '06 #5

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

Aug 12 '06 #6
* 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.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, <algorithmand <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?
Aug 12 '06 #7

Alf P. Steinbach wrote:
>
2. Remove unnecessary headers, <algorithmand <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

Aug 12 '06 #8

Michael wrote:
Alf P. Steinbach wrote:

2. Remove unnecessary headers, <algorithmand <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<intv( 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<intv( 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<Tsubscript

*/

Aug 12 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: TF | last post by:
I have a fixed array of vectors like: vector<string> a_vStr; Then I grow each vector item and use it later. Everything works fine but I am curious about following potential problem. When we...
10
by: BCC | last post by:
Ive been googling and reading through my books but I haven't figured out a solution (much less an elegant one) to create a multidimensional array with a runtime determined number of dimensions. I...
49
by: vfunc | last post by:
If I have a large array 10,000+ elements then how do I reserve memory for this ? Currently I get a segmentation fault. Dynamic reservation is good, but allowing a chunk for the program is an...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
9
by: JoeC | last post by:
I am crating a new version of my map game and my map will be a 2d array. I had problems trying to create a 2d array dynamically, in fact C++ won't let me do it. My question is how to create the...
3
by: Matthias Pospiech | last post by:
I have an 2D vector array, but a function that can calculate only with 1D arrays. Now I need to pass every row of the 2D array to that function. So how can I pass a part (row or column) of a 2D...
7
by: hlg | last post by:
I have a question, which must surely have occurred to many programmers since STL first appeared, and yet I have found no reference to it anywhere, suggesting the problem is insoluble. Nevertheless,...
6
by: remlostime | last post by:
now, i write some code code1: int a; int main(){} code2: vector<inta(10000000); int main(){} after using g++ compile, and run it, code1 is broken, but code2 runs
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.