Vector Constructors.
The available constructors for a vector are given by:
1 vector<int> testVector; 2 vector<long> testVector(10); 3 vector<float> testVector(5,1.0);
The first syntax declares an empty vector capable of storing the integer datatype. The second declares a vector with storage space for 10 long integers, each of which is initialized to the default value for the type. The final line declares a vector with storage for 5 floats, and initializes each of their values to 1.0. Any valid type can be used for any of the constructors.
There is also a copy constructor for the std::vector class. The following code creates a vectors of integers with 10 copies of the number 5, and duplicates the vector into a new one using the copy constructor:
01 #include <iostream> 02 #include <vector> 03 04 using namespace std; 05 06 int main(int argc, char** argv) { 07 08 vector<int> vectorOne(10,5); 09 10 vector<int> vectorTwo(vectorOne); 11 12 return EXIT_SUCCESS; 13 }
Accessing Elements of a Vector.
There are a number of ways to access the elements of a vector. For the moment, I will focus on two of them, one safe and one unsafe. And as a reminder, C++ vectors (and other STL containers), like raw C/C++ arrays, are accessed with indices starting at zero. This means that the first element is at position 0 in the vector, and the last element is at position (number of elements)-1.
The vector class contains a member function at() for accessing individual elements of a vector. This is the safe way of accessing elements, since attempting to access an element beyond the valid range will cause an exception to be thrown. However, the raw data stored in the vector can still be accessed using the usual [] operator, just like in a raw array. Unfortunately, just like with a raw array of data, overrunning the end of the vector using the [] operator can cause weird and unexpected things to occur, such as program crashes or unexpected results. It may also return garbage data that follows the meaningful data of the vector, which has the potential to be disastrous if it is used in subsequent operations. The following two code snippets demonstrate each of these access methods:
Safe access version:
#include <iostream> #include <vector> using namespace std; int main(int argc, char** argv) { /* Initialize vector of 10 copies of the integer 5 */ vector<int> vectorOne(10,5); /* Display size of vector */ cout << "Size of vector is " << vectorOne.size() << " elements." << endl; /* run through the vector and display each element, using size() to determine index boundary */ for (long index=0; index<(long)vectorOne.size(); ++index) { cout << "Element " << index << ": " << vectorOne.at(index) << endl; } return EXIT_SUCCESS; }
Unsafe access version
#include <iostream> #include <vector> using namespace std; int main(int argc, char** argv) { /* Initialize vector of 10 copies of the integer 5 */ vector<int> vectorOne(10,5); /* run through the vector and display each element, if possible */ for (int index=0; index<20; ++index) { cout << vectorOne[index] << endl; } return EXIT_SUCCESS; }
Sources:
http://www.cplusplus.com/reference/vector/vector/vector/
-The Admin.
]]>I had to ask for help to my friend Marco and his blog! He advised me to create the codes and to execute the program and also, a video made by Ken helped me to use the big integer library properly.
Here is the code in my GitHub account!
And of course here is how the program looks like:
Any doubt please let me know!
-The admin.
]]>In the first one we had to write a function called superpower that has two parameters of type long and returns a long which is first parameter raised to the power of the second.
For example, superpower (3, 4) would return 81.
And in the second program we wrote a function called stars that has one parameter of type int and prints (the function does not return anything) that many stars on a single line followed by a end-of-line character, so if we call the function with stars (5), the function will print like this:
*****
At first it was hard to write this codes but then, when I looked for help I could notice that actually they were pretty easy and with a few minutes of reading I could understand a little bit more about how the functions work.
You can check the codes at the bottom of this post or in my GitHub account in the Quizzes Repository, it’s up to you!
Here are the codes of my two programs.
SuperPower!
#include <iostream> #include <cmath> using namespace std; long superpower (long a, long b){ long superpower = 0; superpower = pow(a,b); return superpower; } int main (){ cout<< superpower(3,4) <<endl; return 0; }
Stars!
#include <iostream> using namespace std; void stars(int iStars){ for (int i = 1 ; i <= iStars; i++){ cout << "*"; } cout << endl; } int main(){ stars(5); return 0; }