473,396 Members | 1,871 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,396 software developers and data experts.

a question about STL

JDT
Hi,

I am curious if there is a way to just use existing STL functions,
algorithms etc to accomplish the following loop (without a need to write
my own function). I know how to use some STL functions plus my own
function to replace the loop. But that unnecessarily makes code more
complicated. Any advice is much welcome. Thanks.

int nSize;
....
vector<intv;
v.resize(nSize);
for (int i=0; i<nSize; i++)
v[i] = i;

JD
Apr 13 '07 #1
6 1520
JDT wrote:
I am curious if there is a way to just use existing STL functions,
algorithms etc to accomplish the following loop (without a need to
write my own function). I know how to use some STL functions plus my
own function to replace the loop. But that unnecessarily makes code
more complicated. Any advice is much welcome. Thanks.

int nSize;
...
vector<intv;
v.resize(nSize);
for (int i=0; i<nSize; i++)
v[i] = i;
See 'generate'. Also search the archives for 'generate'. You will
probably need to use some Boost lambda construct to increment 'i'...
Something like that, anyway. In most cases your own function would
still be clearer than the statement you can whip up using standard
library means.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 13 '07 #2
JDT
Victor Bazarov wrote:
JDT wrote:
>>I am curious if there is a way to just use existing STL functions,
algorithms etc to accomplish the following loop (without a need to
write my own function). I know how to use some STL functions plus my
own function to replace the loop. But that unnecessarily makes code
more complicated. Any advice is much welcome. Thanks.

int nSize;
...
vector<intv;
v.resize(nSize);
for (int i=0; i<nSize; i++)
v[i] = i;


See 'generate'. Also search the archives for 'generate'. You will
probably need to use some Boost lambda construct to increment 'i'...
Something like that, anyway. In most cases your own function would
still be clearer than the statement you can whip up using standard
library means.

V
Hi Victor,

Thanks for the reply. But generate uses a customized function as the
3rd parameter, which I avoid because the original code is neater. I am
looking for something such as less<that does such simple things as
increasing. Thanks for any further help.

JD
Apr 13 '07 #3
On Apr 13, 10:13 pm, JDT <jdt_yo...@yahoo.comwrote:
I am curious if there is a way to just use existing STL functions,
algorithms etc to accomplish the following loop (without a need to write
my own function). I know how to use some STL functions plus my own
function to replace the loop. But that unnecessarily makes code more
complicated. Any advice is much welcome. Thanks.
int nSize;
...
vector<intv;
v.resize(nSize);
for (int i=0; i<nSize; i++)
v[i] = i;
You need a special iterator; check out boost::iterators. With
the correct iterator, it's just:

vector<intv( boost::counting_iterator< int >( 0 ),
boost::counting_iterator< int >( nSize ) ) ;
--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 13 '07 #4
JDT
James Kanze wrote:
On Apr 13, 10:13 pm, JDT <jdt_yo...@yahoo.comwrote:

>>I am curious if there is a way to just use existing STL functions,
algorithms etc to accomplish the following loop (without a need to write
my own function). I know how to use some STL functions plus my own
function to replace the loop. But that unnecessarily makes code more
complicated. Any advice is much welcome. Thanks.

>>int nSize;
...
vector<intv;
v.resize(nSize);
for (int i=0; i<nSize; i++)
v[i] = i;


You need a special iterator; check out boost::iterators. With
the correct iterator, it's just:

vector<intv( boost::counting_iterator< int >( 0 ),
boost::counting_iterator< int >( nSize ) ) ;
--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Hi James,

Do you think MS Visual Studio 2005 supports boost? I copied an example
from the Internet but the compiling process failed. For example, the
compiler complained the following file is not found. Your further help
is appreciated.

#include "boost"

JD
Apr 13 '07 #5
JDT wrote:
James Kanze wrote:
>On Apr 13, 10:13 pm, JDT <jdt_yo...@yahoo.comwrote:

>>I am curious if there is a way to just use existing STL functions,
algorithms etc to accomplish the following loop (without a need to write
my own function). I know how to use some STL functions plus my own
function to replace the loop. But that unnecessarily makes code more
complicated. Any advice is much welcome. Thanks.

>>int nSize;
...
vector<intv;
v.resize(nSize);
for (int i=0; i<nSize; i++)
v[i] = i;


You need a special iterator; check out boost::iterators. With
the correct iterator, it's just:

vector<intv( boost::counting_iterator< int >( 0 ),
boost::counting_iterator< int >( nSize ) ) ;
--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Hi James,

Do you think MS Visual Studio 2005 supports boost? I copied an example
from the Internet but the compiling process failed. For example, the
compiler complained the following file is not found. Your further help
is appreciated.

#include "boost"

JD
Boost and MS VC++ are compatible but you need to install boost to make
it work. I've done it before and it's not trivial, however there are
good instructions online. Try searching the boost website for more details.
Apr 13 '07 #6
On Apr 14, 12:51 am, JDT <jdt_yo...@yahoo.comwrote:
James Kanze wrote:
On Apr 13, 10:13 pm, JDT <jdt_yo...@yahoo.comwrote:
>I am curious if there is a way to just use existing STL functions,
algorithms etc to accomplish the following loop (without a need to write
my own function). I know how to use some STL functions plus my own
function to replace the loop. But that unnecessarily makes code more
complicated. Any advice is much welcome. Thanks.
>int nSize;
...
vector<intv;
v.resize(nSize);
for (int i=0; i<nSize; i++)
v[i] = i;
You need a special iterator; check out boost::iterators. With
the correct iterator, it's just:
vector<intv( boost::counting_iterator< int >( 0 ),
boost::counting_iterator< int >( nSize ) ) ;
Do you think MS Visual Studio 2005 supports boost?
Officially, or? It's not included as part of the product, as
far as I know. On the other hand, I'm pretty sure that Boost
supports recent (and even not so recent) VC++.
I copied an example
from the Internet but the compiling process failed. For example, the
compiler complained the following file is not found. Your further help
is appreciated.
Boost uses a somewhat exotic build process, and I'm not familar
with it. However:

-- A large number of the components (including, I suspect,
iterator) are 100% templates, and don't need to be compiled.
Just copy the necessary headers into a Boost directory, and
tell the compiler to look for them there.

-- Some of the components which do require compiling (e.g.
regular expressions, which you'll definitly want as well)
have support for compiling outside of the Boost build
process. You might also want to try that.

Whatever process you use, of course, you'll have to tell the
compiler where to look for the headers and the libraries. For
VC++, it's /I option for the headers, and you specify each
library explicitly, by name. (If you're using bash or ksh, you
can do something like "$BOOSTLIBDIR/*.lib", if you want them
all. Unlike most compilers, the order of libraries is not
significant for VC++.)
#include "boost"
I don't think that there is such a header. Something like:
#include "boost/iterator.hpp"
would be more likely, I think.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 14 '07 #7

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

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
77
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
0
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.