With new. They don't have to be adjacent in memory, that's the point of a linked list. |
So, an array and vector are adjacent, but all other containers aren't. That explains why an array has to be initialized with a const (I know there are workarounds) and a vector resize is processor heavy since it must locate adjacent blocks, correct?
A list is a sequence of pointers, 0-n, that point to values in memory somewhere, so when you access it, it returns the dereferenced value? A list is nothing more than a container of pointers? Are the rest of the STL containers the same way?
Because the left side is a pointer and you want to assign to the object pointed to. |
Something I don't understand is this:
1 2 3
|
*myPointer = value; // set's the value at the memory location of myPointer equal to value
myPointer = &value; // Changes myPointer's memory location to value
// *myPointer than becomes an alias for value?
|
And:
1 2 3
|
char *myPointer, *myPointer2;
*myPointer = *myPointer2 // Only changes the values to be equal?
myPointer = myPointer2; // Sets the memory location of myPointer to the same as myPointer2?
|
I just feel that I have an extra hard time understand exactly the purpose of it. Like, I understand the examples usually well enough, but in practice, it's very hard to use it, and it's even harder for me to read other's code along with coming up with examples on how to use it.
How would you implement operator[] for a list? It would run in linear time. Lists aren't suitable for random access, so there's no real point in providing that operator. |
I just found out about how every container has it's own iterators, and only random access containers are capable of using the [] operator. I still understand the difference between the two, but I can't grasp how a pointer works for the random access containers, but not for linked containers. I feel like a pointer-to-pointer should work, in a sense, as the same as the way a regular pointer works on, say, an array or vector.
There is so many things that I ask about that should make sense. This is one of those times. I didn't know distance existed, but sure enough, it sits right there on the iterator page. I'm kind of disappointed that there isn't just a way to derefence the iterator (maybe this is why I confuse it for a pointer) to get the "index".
Yes, but you can create a foreach + index macro for that. |
Not quite sure what you mean by that. I was thinking about doing a basic for loop to iterate through the elements, but then I still have no idea how to know what "index" or pass on the loop I'm at without declaring a separate variable. I feel, anyways, that this should be something to do easily, at least easier than declaring a separate header for std::distance or creating even another variable (I feel my code creates a lot of unnecessary variables).
Edit: I'm reading Professional C++ and ran across a section about loops and iterations. This one specifically talks about iterators with the new std::array. It says that all STL containers can use the STL algorithms. My question is, on the following, where does iter come from? Specifically, what is the difference between that and i?
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
#include <array>
using namespace std;
int main()
{
array<int, 3> arr = {9, 8, 7};
cout << “Array size = “ << arr.size() << endl;
for (auto i : arr)
cout << i << endl;
cout << *iter << endl;
// Maybe I'm wrong, but shouldn't there be SRO's here?
return 0;
}
|
Edit 2: There was also a segment about using the range based for loop with C style arrays. What type does auto turn into?
1 2 3 4
|
int arr[] = {1, 2, 3, 4};
for(auto& i : arr) {
i += 2;
}
|
Edit: The code in the book was wrong,
cout << *iter << endl;
isn't even supposed to be there.