 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Chris Guest
|
Posted: Sun Jan 29, 2006 9:00 am Post subject: syntax question |
|
|
Hey all. Anyone who is familiar with Python programming knows that you
can have code like this:
list = [x+1 for x in range(5) ]
This code puts all the items processed by the for loop in a list plus
1. Is there a way to do this in C++? Something like:
int list[] = { x + 1 for (int x = 0; x < 5; x++); }
Or something?
Thanks for any help!
-Chris |
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Sun Jan 29, 2006 12:00 pm Post subject: Re: syntax question |
|
|
Chris wrote:
| Quote: | Hey all. Anyone who is familiar with Python programming knows that you
can have code like this:
list = [x+1 for x in range(5) ]
This code puts all the items processed by the for loop in a list plus
1. Is there a way to do this in C++? Something like:
int list[] = { x + 1 for (int x = 0; x < 5; x++); }
|
#include <vector>
int main()
{
std::vector vec;
for (int x = 0; x<5; ++x) vec.push_back(x + 1);
}
Best regards,
Tom |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sun Jan 29, 2006 6:00 pm Post subject: Re: syntax question |
|
|
Thomas Tutone wrote:
| Quote: | Chris wrote:
Hey all. Anyone who is familiar with Python programming knows that
you can have code like this:
list = [x+1 for x in range(5) ]
This code puts all the items processed by the for loop in a list plus
1. Is there a way to do this in C++? Something like:
int list[] = { x + 1 for (int x = 0; x < 5; x++); }
#include <vector
int main()
{
std::vector vec;
|
Did you mean
std::vector<int> vec;
or
std::vector<double> vec;
or
std::vector<long> vec;
or
what???
| Quote: | for (int x = 0; x<5; ++x) vec.push_back(x + 1);
}
|
I strongly recommend you to at least try to compile your code
before posting it. Omissions like that are easily avoidable. |
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Sun Jan 29, 2006 8:00 pm Post subject: Re: syntax question |
|
|
Victor Bazarov wrote:
| Quote: | Thomas Tutone wrote:
#include <vector
int main()
{
std::vector vec;
Did you mean
std::vector<int> vec;
or
std::vector<double> vec;
or
std::vector<long> vec;
or
what???
for (int x = 0; x<5; ++x) vec.push_back(x + 1);
}
I strongly recommend you to at least try to compile your code
before posting it. Omissions like that are easily avoidable.
|
True enough. std::vector<int> vec;
Best regards,
Tom |
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Mon Jan 30, 2006 3:00 am Post subject: was: syntax question |
|
|
In article <1138511019.819692.95160 (AT) g47g2000cwa (DOT) googlegroups.com>,
"Chris" <chrispatton (AT) gmail (DOT) com> wrote:
| Quote: | Hey all. Anyone who is familiar with Python programming knows that you
can have code like this:
list = [x+1 for x in range(5) ]
This code puts all the items processed by the for loop in a list plus
1.
|
In python, you could also do this by:
list = range(1, 6)
| Quote: | Is there a way to do this in C++? Something like:
int list[] = { x + 1 for (int x = 0; x < 5; x++); }
Or something?
|
What about:
vector<int> vec( dat::range(1, 6), dat::range() );
See below for how to do it...
I've made an iterator that works much like python's range function but
is more suited to c++'s algorithms.
Before I show you the class, let me show you how to use it:
int main() {
ostream_iterator<int> os( cout, ", " );
copy( range( 10 ), range(), os ); cout << "\n";
copy( range( 5, 10 ), range(), os ); cout << "\n";
copy( range( 0, 10, 3 ), range(), os ); cout << "\n";
copy( range( -10, -100, -30 ), range(), os ); cout << "\n";
}
compare the examples above to the python page describing range
<http://docs.python.org/tut/node6.html#SECTION006300000000000000000>
Maybe you have a container and you want to find out if any of the
numbers 1 to 5 are in it?
template < typename Container >
void fn( Container c ) {
typename Container::const_iterator it =
find_first_of(c.begin(), c.end(), range(1, 6), range());
if ( it != c.end() ) {
// *it == either 1, 2, 3, 4, or 5
}
}
Is that too cool or what!
---------- range.h ----------
// copywright (c) 2006 by Daniel Tartaglia
// permission to use is granted if notice is given
// email daniel_t (AT) earthlink (DOT) net with "great range" in subject the line
#include <iterator>
#include <stdexcept>
namespace dat {
class range:
public std::iterator<std::bidirectional_iterator_tag, int>
{
int value, end, step;
public:
range(): value(0), end(0), step(0) { }
explicit range(int e): value(0), end(e), step(1) { }
range(int v, int e, int s = 1): value(v), end(e), step(s)
{ if (step == 0) throw std::logic_error("range: bad step"); }
const int& operator*() const { return value; }
range& operator++() { value += step; return *this; }
range& operator--() { value -= step; return *this; }
range operator++(int)
{ range tmp(*this); value += step; return tmp; }
range operator--(int)
{ range tmp(*this); value -= step; return tmp; }
friend bool operator==(const range& lhs, const range& rhs) {
bool result = false;
if (lhs.step == 0 && (rhs.step > 0 && rhs.value >= rhs.end) ||
(rhs.step < 0 && rhs.value <= rhs.end)) result = true;
else if (rhs.step == 0 && (lhs.step > 0 && lhs.value >=
lhs.end) || (lhs.step < 0 && lhs.value <= lhs.end))
result = true;
else if (lhs.step == rhs.step && lhs.value == rhs.value &&
lhs.end == rhs.end) result = true;
return result;
}
friend bool operator<(const range& lhs, const range& rhs) {
bool result = false;
if (lhs.step != 0 && rhs.step == 0) result = true;
else if (lhs.step < 0 && lhs.value > rhs.value) result = true;
else if (lhs.step > 0 && lhs.value < rhs.value) result = true;
return result;
}
};
bool operator!=(const range& lhs, const range& rhs)
{ return !(lhs == rhs); }
}
---------- end range.h ---------- |
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Mon Jan 30, 2006 3:00 am Post subject: Great range iterator was: syntax question |
|
|
In article <1138511019.819692.95160 (AT) g47g2000cwa (DOT) googlegroups.com>,
"Chris" <chrispatton (AT) gmail (DOT) com> wrote:
| Quote: | Hey all. Anyone who is familiar with Python programming knows that you
can have code like this:
list = [x+1 for x in range(5) ]
This code puts all the items processed by the for loop in a list plus
1.
|
In python, you could also do this by:
list = range(1, 6)
| Quote: | Is there a way to do this in C++? Something like:
int list[] = { x + 1 for (int x = 0; x < 5; x++); }
Or something?
|
What about:
vector<int> vec( dat::range(1, 6), dat::range() );
See below for how to do it...
I've made an iterator that works much like python's range function but
is more suited to c++'s algorithms.
Before I show you the class, let me show you how to use it:
int main() {
ostream_iterator<int> os( cout, ", " );
copy( range( 10 ), range(), os ); cout << "\n";
copy( range( 5, 10 ), range(), os ); cout << "\n";
copy( range( 0, 10, 3 ), range(), os ); cout << "\n";
copy( range( -10, -100, -30 ), range(), os ); cout << "\n";
}
compare the examples above to the python page describing range
<http://docs.python.org/tut/node6.html#SECTION006300000000000000000>
Maybe you have a container and you want to find out if any of the
numbers 1 to 5 are in it?
template < typename Container >
void fn( Container c ) {
typename Container::const_iterator it =
find_first_of(c.begin(), c.end(), range(1, 6), range());
if ( it != c.end() ) {
// *it == either 1, 2, 3, 4, or 5
}
}
Is that too cool or what!
---------- range.h ----------
// copywright (c) 2006 by Daniel Tartaglia
// permission to use is granted if notice is given
// email daniel_t (AT) earthlink (DOT) net with "great range" in subject the line
#include <iterator>
#include <stdexcept>
namespace dat {
class range:
public std::iterator<std::bidirectional_iterator_tag, int>
{
int value, end, step;
public:
range(): value(0), end(0), step(0) { }
explicit range(int e): value(0), end(e), step(1) { }
range(int v, int e, int s = 1): value(v), end(e), step(s)
{ if (step == 0) throw std::logic_error("range: bad step"); }
const int& operator*() const { return value; }
range& operator++() { value += step; return *this; }
range& operator--() { value -= step; return *this; }
range operator++(int)
{ range tmp(*this); value += step; return tmp; }
range operator--(int)
{ range tmp(*this); value -= step; return tmp; }
friend bool operator==(const range& lhs, const range& rhs) {
bool result = false;
if (lhs.step == 0 && (rhs.step > 0 && rhs.value >= rhs.end) ||
(rhs.step < 0 && rhs.value <= rhs.end)) result = true;
else if (rhs.step == 0 && (lhs.step > 0 && lhs.value >=
lhs.end) || (lhs.step < 0 && lhs.value <= lhs.end))
result = true;
else if (lhs.step == rhs.step && lhs.value == rhs.value &&
lhs.end == rhs.end) result = true;
return result;
}
friend bool operator<(const range& lhs, const range& rhs) {
bool result = false;
if (lhs.step != 0 && rhs.step == 0) result = true;
else if (lhs.step < 0 && lhs.value > rhs.value) result = true;
else if (lhs.step > 0 && lhs.value < rhs.value) result = true;
return result;
}
};
bool operator!=(const range& lhs, const range& rhs)
{ return !(lhs == rhs); }
}
---------- end range.h ---------- |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|