vector as a pointer using struct C++

Personal contribution.


Can a vector act as a pointer ?
The answer is yes, look at the first and last code (int the reply section).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


#include <iostream>
#include <vector>

using namespace std;

struct example{
  vector <string*> v;
};

int main(){
 example use;
 string a = "Hello";
 use. v.push_back(&a);
for (int i = 0; i < v.size; i++){
 cout << *(use.v[0]);
}
}



However, you can have a pointer that points to an address of a vector, like this example:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>

using namespace std;

struct example{
    string *p = new string;
};

int main(){
    vector<string> myVect = {"Hello"};
    
    example use;
    use. p = &myVect[0];
    cout << *(use. p); // prints Hello
    return 0;
}
}
Last edited on
Heh, I figured it out. Just a syntax error.
A better possible solution to the problem is:


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>

using namespace 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]);
  }
}
Last edited on
Other alternatives would be to use range-loops (C++11) or iterators:
1
2
3
4
5
6
7
8
9
    for (const auto& elem : use.v)
    {
        std::cout << *elem << " ";
    }
    std::cout << '\n';
    for (auto itr = use.v.cbegin(); itr != use.v.cend(); ++itr)
    {
        std::cout << **itr << " ";
    }
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 (const auto &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 ?
Last edited on
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 ?

Last edited on
Did I miss something ?


If you are not going to use pointers then you need to change every expression that uses them.
No I am planning to use pointer of vectors. I thought C++ automatically can convert
vector <string> v; to vector<string*> v;

so I guess I stick to vector<string*> v; , if there is no other way around.
Last edited on
No I am planning to use pointer of vectors.


No, that's why we pass things around with references, as in :

1
2
3
void print_message  (const std::string& message) {
  std::cout << message << "\n";
}
Last edited on
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.
Topic archived. No new replies allowed.