Vectors

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.
#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

  • a vector of vectors
    matrix problems
    templates with generic vectors
  • line 24: in c++11 has a new way to express that using a data type called auto. With auto you don't have to express the data type. Example:
    for(auto x : b)
    {
    
    }
    

    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
    auto i = 0; // an integer because it's initialized with 0
    auto str = "Hello World"; // a std::string because it's initialized with a string
    for(auto i = 0; i < j; ++i)
    
  • Ok that makes sense. Now if I am programming in C do I use the individual data types such as char, int, float, and double, but in C++ I should just use auto in place of those where possible?
  • You can't use auto in C language. You could use auto extensively in c++, but to be honest I would prefer to declare variables the old way. I'm not a big fan of auto except when auto is easier to use than a standard data type, such as iterations. But you can if you want to as long as you yourself don't get confused about what auto is actually declaring.
  • I have noticed one thing about autos so far, and that is you cannot use them like this.
    auto s = "Hello, world, i = 32;// Will give error.
    
  • You cannot use auto to define variables initialized with primitives (ints, floats, chars). This is not only a technical requirement for a statically typed language such as C++ but vital to the code's readability that types be explicitly stated. For practical uses, restrict your usage of auto to only in range based for loops.

    Consider the following:
    std::vector<int> values;
    std::vector<int>::iterator it;
    std::vector<int>::iterator end = values.end();
    
    for(it = values.begin(); it != end; ++it)
    {
        ...
    }
    

    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:
    auto it = values.begin();
    auto end = values.end();
    for(; it != end; ++it)
    {...}
    
    //Combine that with ranged based for loops and you have something that looks like:
    
    for( const auto& it : values)
    {
    ...
    }
    
    



    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.
  • >You cannot use auto to define variables initialized with primitives

    Yes you can (see The C++ Programming Language, 4th Edition). This is perfectly legal
    int main()
    {
        auto s = "Hello, world";
        auto i = 32;
        for(auto j = 0; j < 10; j++)
    }
    

    >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.
  • Post #7 and #8 both have valid points.
    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?
  • Yes, and no. [icode]using namespace std:[/icode] can be dangerous and cause unintended errors when a name in std namespace conflicts with a name you have chosen for your program. That's why it's better to use [icode]std::vector v; [/icode] or [icode]using std::vector[/icode]
  • That is what I though.
    The only thing this auto data type does for my program is this.
    What other vector functions would be good to learn from here?
    #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 (auto 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
    
  • line 24 can be reduced to this:
    for( auto c : b)
    {
        cout << setw (4) << *c << '\n';
    }
    

    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]
  • No it cannot. If I do it the compiler gives me this error.

    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 ==========
  • VS 2010 is not c++11 compliant -- get VS2013 or get latest version of Code::Blocks w/MinGW compiler. I'd suggest CB because VS is not fully c++11 compliant either.
  • I do not think my laptop will use VS2013, and I have never been success full in getting a Code::Block compiler to work. Not to mention I do not like the sound of them anyways.
  • how much RAM and how much free hard drive space does your laptop have? If you can't upgrade then you won't be able to take advantage of any c++11 features.
  • Can you run VS2012? It supports many of the C++11 language features.
  • I have 4.00 GB of RAM (3.48 GB usable)
    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.
  • Also I cannot find anywhere where you can download Visual C++ 2013 all I find is Visual Studio 2013. I don't want the whole thing just a C++ compiler. Which is why I am stuck with 2010 express. None of this stuff has anything to do with vectors by the way. :)
  • I don't think you can just get VC++ 2013, you have to download the entire visual studio 2013 suite.
  • Would that be Visual Studio Express 2013 for Windows Desktop?
  • I downloaded it, but after using it I can no longer find it to open it back up. Also should I uninstall Microsoft Visual C++ 2010 Express 10.0, and if I do that will it take everything it needed with it?
  • Never mind I found the icon, but I am back to the old days of can't get the command prompt to stay open without system ("pause");. Any subjections.
  • Ok nix that as well. I was just setting up the project wrong. With this new C++11 standard are there any uses left over for begin() and end().
  • >With this new C++11 standard are there any uses left over for begin() and end().

    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.
  • When I try to register my new compiler all I get is this. How do I register something if I keeps me from doing so in the first place?
  • Sometimes I get that when the program is busy doing something, such as downloading, and I try to move the window. Just leave it alone for awhile because the install may not be finished yet.
  • It's been the forth of fifth time since I downloaded the compiler yesterday. What more could it have to do?
  • Does your computer meed the minimum requirements stated here?
  • Here is everything minus some things I took out. :P Is it good enough?
Sign In or Register to comment.