You can use unsigned int i = 0; or you can use std::size_t i = 0; (They are not the same thing, but they give the same output for this simple program).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <vector>
usingnamespace std;
struct example{
vector <string*> v;
};
int main(){
example use;
string a = "Hello";
use. v.push_back(&a);
for (std::size_t i = 0; i < use.v.size(); i++){
cout << *(use.v[0]);
}
}
yeah considering that use.v has been given an element of type string before the for range-loop, everything works perfectly fine.
1 2 3 4 5 6 7
x use;
string a = "Bye";
use. v.push_back(&a);
for (constauto &s : use.v){
cout << *s << " ";
}
cout << endl;
Just a quick question @gunnerfunner, why did use use cbegin() and cend(); To me, it seems like you are trying to sort the vector of pointers, but why ?
You can use unsigned int i = 0; or you can use std::size_t i = 0; (Both are the same).
No they are not. std::size_t is usually the largest unsigned type.
vector <string*> v;
There is no need to do that. STL containers and the std::string class already store a pointer to their data on the heap.
One of the best things about the STL, is to get away from all that pointer and memory management stuff. Don't mix C and C++ - they are quite different paradigms.
@gunnerfunner, thanks, I'll dig further into that.
@TheIdeasMan,
oh I see, yeah since from the last discussion I felt like they might be the same since they give the same output, but it seems they are different in meaning.
Also, I tried it without the * like vector <string> v; , but it did not work. Did I miss something ?
Oh yes, I mean using a pointer that exists in a struct, and then point it to the address of strings using pass by reference like the example you have provided. That is indeed correct.