where I can find good tutorials on Vector?(newbie)

S

sam

where I can find good tutorials on Vector?(newbie)?
I have 3 books on c++:-
Let us C++,
c++ Introuduction to Programming by Jim Keough and Jesse Liberty
and one book on beginner C
But that are not included Vector topic.
Please suggest me the web resources on it.
 
D

David Harmon

On 18 Nov 2006 10:46:05 -0800 in comp.lang.c++, "(e-mail address removed)"
google: STL vector

Are you saying that all of the results from that search will be "good
tutorials" on std::vector, or just the first one?
 
A

Alf P. Steinbach

* sam:
where I can find good tutorials on Vector?(newbie)?
I have 3 books on c++:-
Let us C++,
c++ Introuduction to Programming by Jim Keough and Jesse Liberty
and one book on beginner C
But that are not included Vector topic.
Please suggest me the web resources on it.

Here's a good tutorial on std::vector:

1. Use 'at' instead of '[]' (except perhaps after profiling).
2. To really clear a vector v, including the allocated buffer, do
std::vector<T>().swap(v). Note that without such clearing the
buffer usually just increases and increases in size, over time.
3. To initialize a vector v with constant data, use
std::vector<T> v( data, data+n )
where data is a static array and n is the number of items.
4. To initialize all elements with a given value, use
std::vector<T> v( n, value );
5. An iterator is not a pointer even if your compiler implements it
that way.

Hth.,

- Alf
 
B

BobR

sam wrote in message
where I can find good tutorials on Vector?(newbie)?
I have 3 books on c++:-
Let us C++,
c++ Introuduction to Programming by Jim Keough and Jesse Liberty
and one book on beginner C
But that are not included Vector topic.
Please suggest me the web resources on it.

This NewsGroup IS ONE BIG tutorial!! And plenty of 'vector' topics.

The comp.lang.c++ FAQ is available at http://www.parashift.com/c++-faq-lite/
There are some helpful things in it.


Get "Thinking in C++", 2nd ed. Volume 1(&2) by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
 
S

Steve Pope

Alan said:
Alf P. Steinbach wrote:
1. Use 'at' instead of '[]' (except perhaps after profiling).
2. To really clear a vector v, including the allocated buffer, do
std::vector<T>().swap(v). Note that without such clearing the
buffer usually just increases and increases in size, over time.
Alf,
Could you explain why you advise #1 [snip] and what is going
on in #2?

I'm curious as well. What is wrong with resize(0) ?

(Tangentially, I keep reading the subject line of this thread
as containing vector<newbie>.)

S.
 
K

Kai-Uwe Bux

Steve said:
Alan said:
Alf P. Steinbach wrote:
1. Use 'at' instead of '[]' (except perhaps after profiling).
2. To really clear a vector v, including the allocated buffer, do
std::vector<T>().swap(v). Note that without such clearing the
buffer usually just increases and increases in size, over time.
Alf,
Could you explain why you advise #1 [snip] and what is going
on in #2?

I'm curious as well. What is wrong with resize(0) ?

Nothing. However, in the most natural implementation, resizing down will
keep the currently allocated buffer.

[snip]


Best

Kai-Uwe Bux
 
B

BobR

Alf P. Steinbach wrote in message said:
Here's a good tutorial on std::vector:

1. Use 'at' instead of '[]' (except perhaps after profiling).

<choke>
...or when you're positive of the index...

std::vector<int> Vint(10);
for( size_t I(0); i < Vint.size(); ++i ){
std::cout << Vint[ i ] << std::endl;
// std::cout << Vint.at( i ) << std::endl; // if 'i<Vint.size()' not used.
}

The <choke> was because I usually push '.at()' at people, and defending the
'[]' stuck in my throat! <G>
 
A

Alan

OK, I see. By "clear" you mean clearing the capacity, too. If I
want to maintain the size allocation but just clear the data, I can use
..clear().

Alf P. Steinbach wrote:
2. To really clear a vector v, including the allocated buffer, do
std::vector<T>().swap(v). Note that without such clearing the
buffer usually just increases and increases in size, over time.
 
D

David Harmon

On 18 Nov 2006 15:43:42 -0800 in comp.lang.c++, "Alan"
I have been using .clear(), but I`m still pretty new to vectors, too.

..clear() is often a reasonable thing to do, but it doesn't release any
memory in the typical vector implementation. If you are not going to
use the vector again, then the program logic probably calls for it to go
out of scope or otherwise get destroyed soon anyway, and that releases
everything. If you are going to use it again, it's fairly likely that
you will use about the same amount of memory the second time, so just
clearing it may save a bunch of messy memory allocation.
 
B

BobR

Steve Pope wrote in message ...
Alan said:
Alf P. Steinbach wrote:
1. Use 'at' instead of '[]' (except perhaps after profiling).

See my other post in this thread.

{ // main() or function
try{
std::vector<int> Vint( 2 );
std::cout<< Vint.at( 3 ); // out_of_range
} // try
catch( const std::eek:ut_of_range &Oor ){
std::cout<<"caught: "<<Oor.what()<<std::endl;
}
catch( ... ){ // catch anything not caught above.
std::cout<<"caught something (maybe the flu!!)"<<std::endl;
}
}
Alf,
Could you explain why you advise #1 [snip] and what is going
on in #2?

I'm curious as well. What is wrong with resize(0) ?

(Tangentially, I keep reading the subject line of this thread
as containing vector<newbie>.)
S.

// Prove it for yourself:

{ // main() or function

std::vector<double> values(10);

std::cout << values.size() << std::endl;
std::cout << values.capacity() << std::endl;

values.clear();
// values.resize(0); // same

std::cout << values.size() << std::endl; // S/B '0'
std::cout << values.capacity() << std::endl; // clear() doesn't re-size

std::vector<double>( ).swap( values );
// You are swapping an empty (un-named) vector with your vector 'values'.

std::cout << values.size() << std::endl; // S/B '0'
std::cout << values.capacity() << std::endl; // S/B '0'

}
 
M

Mike Wahler

Alan said:
Alf,
Could you explain why you advise #1 (below) and what is going on
in #2?

Thanks, Alan
1. Use 'at' instead of '[]' (except perhaps after profiling).

If you compare the documentation of 'at()' with '[]', you'll
find that if an out-of-bounds index is used, 'at()' will throw
an exception, while '[]' will produce undefined behavior. Initially
using 'at()' will help locate logical errors, with well defined behavior.

Here, Alf is referring to allocated memory. vector::clear() will erase
all elements, but won't necessarily release the memory they occupied.
Using 'swap' to replace the old vector with an empty one will. This
may or may not be a concern for you.

-Mike
 

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top