a question about STL

Discussion in 'C++' started by JDT, Apr 13, 2007.

  1. JDT

    JDT Guest

    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<int> v;
    v.resize(nSize);
    for (int i=0; i<nSize; i++)
    v = i;

    JD
     
    JDT, Apr 13, 2007
    #1
    1. Advertising

  2. 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<int> v;
    > v.resize(nSize);
    > for (int i=0; i<nSize; i++)
    > v = 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
     
    Victor Bazarov, Apr 13, 2007
    #2
    1. Advertising

  3. JDT

    JDT Guest

    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<int> v;
    >>v.resize(nSize);
    >>for (int i=0; i<nSize; i++)
    >> v = 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
     
    JDT, Apr 13, 2007
    #3
  4. JDT

    James Kanze Guest

    On Apr 13, 10:13 pm, 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<int> v;
    > v.resize(nSize);
    > for (int i=0; i<nSize; i++)
    > v = i;


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

    vector<int> v( boost::counting_iterator< int >( 0 ),
    boost::counting_iterator< int >( nSize ) ) ;


    --
    James Kanze (Gabi Software) email:
    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
     
    James Kanze, Apr 13, 2007
    #4
  5. JDT

    JDT Guest

    James Kanze wrote:

    > On Apr 13, 10:13 pm, 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<int> v;
    >>v.resize(nSize);
    >>for (int i=0; i<nSize; i++)
    >> v = i;

    >
    >
    > You need a special iterator; check out boost::iterators. With
    > the correct iterator, it's just:
    >
    > vector<int> v( boost::counting_iterator< int >( 0 ),
    > boost::counting_iterator< int >( nSize ) ) ;
    >
    >
    > --
    > James Kanze (Gabi Software) email:
    > 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
     
    JDT, Apr 13, 2007
    #5
  6. JDT

    Mark P Guest

    JDT wrote:
    > James Kanze wrote:
    >
    >> On Apr 13, 10:13 pm, 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<int> v;
    >>> v.resize(nSize);
    >>> for (int i=0; i<nSize; i++)
    >>> v = i;

    >>
    >>
    >> You need a special iterator; check out boost::iterators. With
    >> the correct iterator, it's just:
    >>
    >> vector<int> v( boost::counting_iterator< int >( 0 ),
    >> boost::counting_iterator< int >( nSize ) ) ;
    >>
    >>
    >> --
    >> James Kanze (Gabi Software) email:
    >> 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.
     
    Mark P, Apr 14, 2007
    #6
  7. JDT

    James Kanze Guest

    On Apr 14, 12:51 am, JDT <> wrote:
    > James Kanze wrote:
    > > On Apr 13, 10:13 pm, 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<int> v;
    > >>v.resize(nSize);
    > >>for (int i=0; i<nSize; i++)
    > >> v = i;


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


    > > vector<int> v( 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:
    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
     
    James Kanze, Apr 14, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Bruce

    To STL or not to STL

    Allan Bruce, Oct 16, 2003, in forum: C++
    Replies:
    41
    Views:
    1,121
    Christopher Benson-Manica
    Oct 17, 2003
  2. Replies:
    4
    Views:
    829
    Daniel T.
    Feb 16, 2006
  3. Replies:
    2
    Views:
    574
    klaus hoffmann
    Feb 22, 2006
  4. Replies:
    5
    Views:
    534
    Markus Schoder
    Apr 16, 2006
  5. Steve
    Replies:
    2
    Views:
    538
    Andre Kostur
    Nov 6, 2007
Loading...

Share This Page