![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Nov 2004
Location: Bierut - Lebanon
Posts: 34
Rep Power: 0
![]() |
hi
i'm reading the book : "Teach yourself C++ in 21 days" and i heard about vectors , but the book doesnot have any explanation about vectors may any one here please give me a link or a little tutorial about vectors , just to get the idea B) thanks in advance ![]()
__________________
<removed by Administrator> |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Dec 2004
Posts: 35
Rep Power: 0
![]() |
|
|
|
|
|
|
#3 |
|
Expert Programmer
|
You can also get more information by searching for "vector class" in MSDN, or at http://msdn.microsoft.com
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Nov 2004
Location: Bierut - Lebanon
Posts: 34
Rep Power: 0
![]() |
ok thanks for the following links but they did'nt do me any good since i don't know what vectors are in the first place ! :wacko:
these link might be usefull if i know what vectors are first
__________________
<removed by Administrator> |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
i believe they're like dynamically-sized arrays (i.e. change size automatically to fit your needs).
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Dec 2004
Posts: 35
Rep Power: 0
![]() |
>these link might be usefull if i know what vectors are first
It's basically a dynamic array class. The name vector was used because array is a common class name and vector is a convenient mathematical term. If you wanted, you could write your own (very!) simple vector class like so: #include <cstdlib>
#include <iostream>
using namespace std;
template <typename T>
class Vector {
public:
Vector();
~Vector();
public:
const T operator[](size_t sub) const { return data[sub]; }
T& operator[](size_t sub) { return data[sub]; }
void push_back(T obj);
size_t size() const { return data_size; }
private:
T *data;
size_t data_size;
size_t block_size;
};
template <typename T>
Vector<T>::Vector(): data(0), data_size(0), block_size(0)
{}
template <typename T>
Vector<T>::~Vector()
{
delete [] data;
}
template <typename T>
void Vector<T>::push_back(T obj)
{
// Grow if necessary
if (data_size == block_size) {
block_size = (block_size == 0) ? 1 : block_size * 2;
T *new_data = new T[block_size];
for (size_t i = 0; i < data_size; i++)
new_data[i] = data[i];
delete [] data;
data = new_data;
}
// Append
data[data_size++] = obj;
}
int main()
{
Vector<int> test;
for (int i = 0; i < 10; i++)
test.push_back(i);
for (size_t i = 0; i < test.size(); i++)
cout<< test[i] <<' ';
cout<<endl;
for (size_t i = 0; i < test.size(); i++)
++test[i];
for (size_t i = 0; i < test.size(); i++)
cout<< test[i] <<' ';
cout<<endl;
} |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
that sounds a lot like re-inventing the wheel.
programming term...(already been done)...meaning you're wasting your time (unless you're doing it to learn, then it's all good). good luck.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Dec 2004
Posts: 35
Rep Power: 0
![]() |
>that sounds a lot like re-inventing the wheel.
What does? Writing a toy vector class to help someone understand what it is? Or the standard vector class trying to replace arrays? |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Nov 2004
Location: Bierut - Lebanon
Posts: 34
Rep Power: 0
![]() |
thanks for all the replys
and i'm glad to get these examples ![]()
__________________
<removed by Administrator> |
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
[/quote] >that sounds a lot like re-inventing the wheel.
What does? Writing a toy vector class to help someone understand what it is? Or the standard vector class trying to replace arrays?[quote] sounds a lot like i was drunk.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|