Vectors
in C++
Ever since I learned I know nothing about vectors I have been trying to learn about them. So far this is all I have done, and it was kind of fun. I am sure there is more to learn about them, but learning all the functions in alphabetic order would never work.
Where do I go from here?
What could be better?
etc.
Where do I go from here?
What could be better?
etc.
#include <iostream>
#include <ctime>
#include <iomanip>
#include <vector>// Learning vectors.
using std::cout;
using std::setw;
using std::vector;
int main()
{
srand ((unsigned int) time (NULL));// Seed.
int a = rand() % 100 + 1;// Random integer.
vector <int> b;// Allocate.
b.push_back (8);
b.push_back (16);
b.push_back (32);
b.push_back (64);
b.push_back (128);
b.push_back (256);
b.push_back (512);
b.push_back (1024);
for (vector <int> ::const_iterator c = b.begin(); c != b.end(); c++)
{
cout << setw (4) << *c << '\n';
}
if (b.empty())
{
cout << "Empty.\n";
}
else
{
cout << "Vector size: " << b.size() << '\n';
}
b.pop_back();// Takes a vector away.
cout << "pop_back() is used.\n";
if (b.empty())
{
cout << "Empty.\n";
}
else
{
cout << "Vector size: " << b.size() << '\n';
}
b.clear();// Reallocate.
b.push_back (a);
cout << b [0] << '\n';
if (b.empty())
{
cout << "Empty.\n";
}
else
{
cout << "Vector size: " << b.size() << '\n';
}
}// main.cpp
Comments
matrix problems
templates with generic vectors
The above is the same as what you wrote on line 24 but with a lot less typing and complexity. AFAIK you can use auto anyplace a data type is needed as long as it is initialized with something
Consider the following:
Here is where auto shines. Its really frustrating to have to write out all of the declaration [icode]std::vector<int>::iterator end = values.end();
[/icode] The argument is that the compiler /knows/ what the data type returned by end() is. It should know what my intention is and I shouldnt have to type it all out.
Thats why auto was added. To turn that into this:
Now consider a use like yours [icode]auto i = 10;[/icode]. Should this be an int? A long? A float? It could even be a char. Its simply not clear. That is why auto cannot be used in this way.
Yes you can (see The C++ Programming Language, 4th Edition). This is perfectly legal
>I have noticed one thing about autos so far, and that is you cannot use them like this.
Yes, they have to be on separate lines.
Now the only other question I have is about using namespace std; vs using std::vector;.
Books I have and places around the internet are claiming when you use vectors you should start using the using namespace std; instead using std::vector;.
Is this true?
The only thing this auto data type does for my program is this.
What other vector functions would be good to learn from here?
vectors are really not much more than smart arrays. So anything you can do with an array you can do with a vector. Another thing to try is to simulate a 2d array with vectors
[icode]vector< vector<int> > b;[/icode]
1>
Build started: Project: C++, Configuration: Debug Win32
1> C++.cpp
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c++\c++\c++.cpp(24): error C2143: syntax error : missing ',' before ':'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c++\c++\c++.cpp(24): error C3531: 'c': a symbol whose type contains 'auto' must have an initializer
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c++\c++\c++.cpp(25): error C2143: syntax error : missing ';' before '{'
1>c:\users\smjprogrammer\documents\visual studio 2010\projects\c++\c++\c++.cpp(26): error C2100: illegal indirection
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have 219 GB of Hard Drive space of 273 GB.
I am pretty sure I tried to install Microsoft Visual C++ 2012 on this laptop when I first got it a over a year ago, and it would not even download properly. Something about how it was not compatible which seemed wired cause I just bought the think. You would think it would but it does not. If you want to know anything about my laptop you can Google the top portion of my signature.
However I have downloaded different anti virus software, for I believe the other was interfering with my programming anyways.
Yes, certainly when function parameters require it, such as std::sort
Note that VS 2013 is not fully c++11 compatible. I'm reading The C++ Programming Language 4th Edition and have found some things that VC++ 2013 won't compile, but Code::Blocks w/MinGW compiler will compile ok.