Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > My first project

Reply
Thread Tools

My first project

 
 
r
Guest
Posts: n/a
 
      12-21-2006
Caleb wrote:
>
> I thought of a way I could set it up where I can add multiple elements
> with one statement....rather simple, really. Just overload an
> "add_doors" function with one integer, two integers, five integers, and
> whatever amounts of integers that I need, and write it to push_back the
> elements that were provided one at a time. Contemplating whether that
> would really be worth it, though.


There is an 'assign' library in boost. It may be what you are looking
for.

 
Reply With Quote
 
 
 
 
Caleb
Guest
Posts: n/a
 
      12-21-2006
My latest problem: All commands are entered by an integer selected
from a menu (or "0" for quit). If something invalid is entered, like
"x" or ".", the program enters a neverending loop. This seems to be a
common problem for n00bs as I have researched it on Google. I have
found workarounds to detect invalid input, but they all seem to cause
"0" and invalid input to be read the same. The value is being read to
an "int" variable. Can anyone help with that one? Thanks again for
all the help.

 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      12-21-2006
Caleb a écrit :
> My latest problem: All commands are entered by an integer selected
> from a menu (or "0" for quit). If something invalid is entered, like
> "x" or ".", the program enters a neverending loop. This seems to be a
> common problem for n00bs as I have researched it on Google. I have
> found workarounds to detect invalid input, but they all seem to cause
> "0" and invalid input to be read the same. The value is being read to
> an "int" variable. Can anyone help with that one? Thanks again for
> all the help.
>

http://www.parashift.com/c++-faq-lit....html#faq-15.3
 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      12-21-2006
Caleb a écrit :
> std::vector does exactly what I want it to do. It seems to be
> extremely advantageous over regular arrays, too. There's just one
> thing I'm wondering....is there a way to push_back three elements at
> once? For example, I'm going to have a list of door initializations
> that look like this:
>
> Foyer.destination.push_back(2);Foyer.destination.p ush_back(3);Foyer.destination.push_back(6);
>
> And that's only for a 3-door room....is there a more efficient way to
> code that?
>

Certainly:
- you can read your data from a file and insert it into a vector
- you can use arrays (http://www.boost.org/doc/html/array/):
boost::array<int,3> foyer_dest = { 2, 3, 6 } ;
Foyer.destination.insert(foyer_dest.begin(),foyer_ dest.end());
 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      12-21-2006

Caleb wrote:
> std::vector does exactly what I want it to do. It seems to be
> extremely advantageous over regular arrays, too. There's just one
> thing I'm wondering....is there a way to push_back three elements at
> once? For example, I'm going to have a list of door initializations
> that look like this:
>
> Foyer.destination.push_back(2);Foyer.destination.p ush_back(3);Foyer.destination.push_back(6);
>
> And that's only for a 3-door room....is there a more efficient way to
> code that?


You can use something like the code below. Something similar may even
be part of the next version of the C++ standard. Code separates into a
for_each that works on containers or arrays and some custom functors as
examples to use with it. push_back loads a container and output sends
it to output stream. functions below functors just make each functor a
bit more convenient to use.
Can also define your own functors too...

regards
Andy Little

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

//container for_each
template <typename Container, typename F>
void for_each(Container & c, F const & f)
{
std::for_each(c.begin(),c.end(),f);
}

//array version of for_each
template <typename T,int N,typename F>
void for_each(T (&ar)[N],F const & f)
{
for (int i = 0; i < N;++i){
f(ar[i]);
}
}

// use with foreach to load a std container
// from another container or an array
template <typename Container>
struct push_back_{
Container & c;
push_back_(Container & c_in):c(c_in){}

template <typename T>
void
operator()(T const & t)const
{
return c.push_back(t);
}
};
// function returns functor above
template <typename Container>
inline
push_back_<Container> push_back(Container & c)
{
return push_back_<Container>(c);
}

// do output
template <typename CharType>
struct output_{
std::basic_ostream<CharType> & os;
CharType sep;
output_(
std::basic_ostream<CharType> & os_in,
CharType sep_in)s(os_in),sep(sep_in){}

template <typename T>
std::basic_ostream<CharType> &
operator()(T const & t)const
{
return os << t << sep;
}
};
// function returns functor above
template <typename CharType>
inline
output_<CharType>
output(std::basic_ostream<CharType>& os, CharType sep)
{
return output_<CharType>(os,sep);
}

// etc


int main()
{
int array [] = {1,2,3,4,5};

std::vector<int> vect;

for_each(array,push_back(vect));

for_each(vect,output(std::cout,'\n'));
}

 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      12-21-2006
kwikius a écrit :
> Caleb wrote:
>> std::vector does exactly what I want it to do. It seems to be
>> extremely advantageous over regular arrays, too. There's just one
>> thing I'm wondering....is there a way to push_back three elements at
>> once? For example, I'm going to have a list of door initializations
>> that look like this:
>>
>> Foyer.destination.push_back(2);Foyer.destination.p ush_back(3);Foyer.destination.push_back(6);
>>
>> And that's only for a 3-door room....is there a more efficient way to
>> code that?

>
> You can use something like the code below. Something similar may even
> be part of the next version of the C++ standard. Code separates into a
> for_each that works on containers or arrays and some custom functors as
> examples to use with it. push_back loads a container and output sends
> itc to output stream. functions below functors just make each functor a
> bit more convenient to use.
> Can also define your own functors too...
>
> [code]
>
>
> int main()
> {
> int array [] = {1,2,3,4,5};
>
> std::vector<int> vect;
>
> for_each(array,push_back(vect));
>
> for_each(vect,output(std::cout,'\n'));
> }
>


Yes, you can use the for_each structure but it is expected to be less
efficient than the std::vector::insert method.

I don't see the advantage of defining a for_each specifically for arrays
since there is boost::array that works fine (even though it is not
really a container).

And using POD:
int array [] = {1,2,3,4,5};
std::vector<int> vect;
//should work
vect.insert(&array[0],&array[sizeof(array)/sizeof(int)]);
// if you use gcc:
vect.insert(normal_iterator(&array[0]),
normal_iterator(&array[sizeof(array)/sizeof(int)]));



Alternatively, you can use lambda programming:
http://www.boost.org/doc/html/lambda.html


 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      12-21-2006

Michael DOUBEZ wrote:

> And using POD:
> int array [] = {1,2,3,4,5};
> std::vector<int> vect;
> //should work
> vect.insert(&array[0],&array[sizeof(array)/sizeof(int)]);


hmm.... clearly superior to:

for_each(array,push_back(vect));



regards
Andy Little

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      12-21-2006

Michael DOUBEZ wrote:

> std::vector<int> vect;
> //should work
> vect.insert(&array[0],&array[sizeof(array)/sizeof(int)]);


FWIW output with your version in VC7.1, gcc4.1 and VC8 ---> below.
Which vector::insert signature is it meant to be using?

regards
Andy Little

/*
..
..
..
*/

int main()
{
int array [] = {1,2,3,4,5};

std::vector<int> vect;

/* for_each(array,push_back(vect)); */
vect.insert(&array[0],&array[sizeof(array)/sizeof(int)]);
}

Test.cpp
VC7.1 output:

d:\Projects\Test\Test.cpp(80) : error C2664:
'std::vector<_Ty>::iterator
std::vector<_Ty>::insert(std::vector<_Ty>::iterato r,const _Ty &)' :
cannot convert parameter 2 from 'int *__w64 ' to 'const int &'
with
[
_Ty=int
]
Reason: cannot convert from 'int *__w64 ' to 'const int'
This conversion requires a reinterpret_cast, a C-style cast or
function-style cast

/////////////

gcc 4.1 output:


test.cpp: In function 'int main()':
test.cpp:80: error: no matching function for call to 'std::vector<int,
std::allo
cator<int> >::insert(int*, int*)'
/opt/conceptgcc-4.1.1-alpha-4/lib/gcc/i686-pc-cygwin/4.1.1/../../../../include/c
++/4.1.1/bits/vector.tcc:93: note: candidates are: typename
std::vector<_Tp, _Al
loc>::iterator std::vector<_Tp,
_Alloc>::insert(__gnu_cxx::__normal_iterator<typ
ename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type:ointer,
std::vector<_Tp,
_Alloc> >, const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]
/opt/conceptgcc-4.1.1-alpha-4/lib/gcc/i686-pc-cygwin/4.1.1/../../../../include/c
++/4.1.1/bits/stl_vector.h:651: note: void std::vector<_Tp,
_Alloc>::insert(__g
nu_cxx::__normal_iterator<typename std::_Vector_base<_Tp,
_Alloc>::_Tp_alloc_typ
e:ointer, std::vector<_Tp, _Alloc> >, size_t, const _Tp&) [with _Tp =
int, _Al
loc = std::allocator<int>]
make: *** [test.o] Error 1

//////////////////

VC8,=.o output:

..\test.cpp(80) : error C2664: 'std::_Vector_iterator<_Ty,_Alloc>
std::vector<_Ty>::insert(std::_Vector_iterator<_Ty ,_Alloc>,const _Ty
&)' : cannot convert parameter 1 from 'int *' to
'std::_Vector_iterator<_Ty,_Alloc>'
with
[
_Ty=int,
_Alloc=std::allocator<int>
]
No constructor could take the source type, or constructor
overload resolution was ambiguous

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      12-21-2006

Michael DOUBEZ wrote:

> Alternatively, you can use lambda programming:
> http://www.boost.org/doc/html/lambda.html


I would be interested to see what the boost lambda version looks like
too. Not knocking it. Just have no idea. Anyone care to reveal it ?

regards
Andy Little

 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      12-21-2006

Michael DOUBEZ wrote:

> I don't see the advantage of defining a for_each specifically for arrays
> since there is boost::array that works fine (even though it is not
> really a container).


I covered that in the example code FWIW. If you want to use
boost::array ( or another container) you can do, but you have to
specify the size, which can be inconvenient when adding stuff to the
initialiser list.


int main()
{
// int array [] = {1,2,3,4,5};

boost::array<int,5> array = {1,2,3,4,5};

std::vector<int> vect;

for_each(array,push_back(vect));

for_each(array,output(std::cout,'\n'));

}

regards
Andy Little

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
70-290 first or 70-291 first? MCSE 2 07-11-2006 03:30 AM
I'm lazy: how do I make the first databound record not display/chop off the first element from SqlDataSource ASP .Net 7 06-28-2006 10:24 AM
20D's first field trip. First Snake of Spring Celtic Boar Digital Photography 3 04-10-2005 09:40 PM
help with my first project on first job, how to read a strange file, thanks a lot!!!!!!! matt Java 9 10-27-2004 03:32 AM
FS: First Clamshell Laptop - First Laptop in Space - GRiD 1101 Compass Computer Dave Computer Information 0 09-22-2004 10:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57