Double to string conversion

B

BobR

Gavin Deane wrote in message
#include <vector>
typedef std::vector<std::vector<std::vector<int> > > vec3d;
const int dimensions = 5;
const int grid_points = 42;
const int the_third_one = 7;

Small (somewhat) safety issue; Those should be unsigned, not int.

const int dimensions = -5; // ouch, a HUGE size vector.
// vector ctor interprets it as unsigned.

const size_t dimensions = -5; // let the compiler warn first.
// I guess 'std::vector::size_type' would be even better (?).

to OP: the 'const' isn't required here, just good practice.
size_t dimensions( 5 );
std::vector<int> vecA(dimensions, int(42));
dimensions = 22;
std::vector said:
int main(){
vec3d bar(dimensions, std::vector<std::vector<int> >(grid_points,
std::vector<int>(the_third_one)));

// I guess that answers my question in my other post. Thanks!
}

Gavin Deane

Just a thought.
 
G

Gavin Deane

BobR said:
Gavin Deane wrote in message


Small (somewhat) safety issue; Those should be unsigned, not int.

const int dimensions = -5; // ouch, a HUGE size vector.
// vector ctor interprets it as unsigned.

const size_t dimensions = -5; // let the compiler warn first.
// I guess 'std::vector::size_type' would be even better (?).

The vector constructor expects std::vector::size_type so that would be
the best choice, assuming you don't think you might be changing the
container to a deque or something later.
to OP: the 'const' isn't required here, just good practice.
size_t dimensions( 5 );
std::vector<int> vecA(dimensions, int(42));
dimensions = 22;
std::vector<int> vecB(dimensions);

Yes, another advantage of vectors is that you don't need to know the
sizes at compile time. But the discussion with the OP so far had been
around the concept of compile time constants. I just didn't want to add
too much information and risk confusion.

Gavin Deane
 

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,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top