The difference in syntax between a list and a tuple is that to create a list you use “[ ]” and to create a tuple you use “( )”. Plus tuples are defined as “Heterogeneous data structures” and lists are defined as “Homogeneous sequences that have an order”
The use you most probably will use the tuple as reference data, since the values inside it cannot be modified; the list in the other hand can have the values inside it changed, move around, add new values and eliminate existing ones.
If you remember the mastery about “For” loops, in the example we used a list named “L” containing the values [1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 5]. For that program we could use a tuple as well since the “for” only checks for the value within the list, but it doesn’t modify it.
There are a whole bunch of things you can do with lists and tuples, like splitting and indexing them, for a whole list of the things you can do and how to do them please visit this link: http://www.tutorialspoint.com/python/python_lists.htm
You should be very careful not to use lists instead tuples or the other way around since even if you ARE able to do some things with both without negative results, not all of their functions are the same, tuples are more oriented towards indexing and lists are fore managing data.
Lists and tuples have positions, or spaces, and they store the values they are given in those. Those positions start with 0, then 1, next 2 and so on till they cover the length they are needed. So if you need the value stored in the tenth position of a list, you would need to call for the ninth value, since the counter starts from zero instead of one.
Another important thing is that they can contain any amount of data, and they don’t care about dta types, you can just throw integers, floats and strings inside a list and it will be just fine, just be careful when trying to mess around with the information itself, the list doesn’t have a problem with it but other parts of your code may.
]]>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.
]]>