-1

I have a vector which holds a structure. Ex:

structure A { int x; int y; int z;)
vector<A> myVectorA;

Now that i have a requirement for a function which tells me to return the 0-based indices of myVectorA and function prototype is as follows.

vector<size_t> get_A() const
{

}

I am the beginner and not sure what is intended of this function and what to return from this function that matches the return type of vector<size_t>.

Can somebody help.

Editing to Add more information.

My vector myVectorA is a public class member and it needs to be returned from vector<size_t> get_A() const function. But looking at the function prototype, it returns vector<size_t> So my question is how to convert a vector of type Structure A (myVectorA here) to another vector of type vector<size_t>

1 Answers1

-1

Your question is not clear enough. See if the below answer is what you require. In the below function get_A will return all elements of myvectorA in format [x0,y0,z0,x1,y1,z1,...].

#include <bits/stdc++.h>
using namespace std;

struct A { int x; int y; int z;};


vector<size_t> get_A(vector<A> myVectorA)
{
    vector<size_t>a;
    for(int i=0;i<myVectorA.size();i++){
        a.push_back(myVectorA[i].x);
        a.push_back(myVectorA[i].y);
        a.push_back(myVectorA[i].z);
     }
    return a;

}

int main() {
    vector<A>myVectorA;
    A a{1,2,3};
    A b{4,5,6};
    A c{7,8,9};
    myVectorA.push_back(a); // myVectorA now becomes - [{1,2,3}]
    myVectorA.push_back(b);
    myVectorA.push_back(c);// now I have pushed 3 instances of struct A. myVectorA now becomes - [{1,2,3},{4,5,6},{7,8,9}]

    vector<size_t>res = get_A(myVectorA); //will return [1,2,3,4,5,6,7,8,9]
    for(int i=0;i<res.size();i++)
        cout<<res[i]<<" ";     //prints all elements of myVectorA
    return 0;
}

OUTPUT -

1 2 3 4 5 6 7 8 9 
Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • there is an error in your code that gets apparent once you try it with more than one point in `myVectorA` – 463035818_is_not_an_ai May 26 '20 at 12:29
  • and please read [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – 463035818_is_not_an_ai May 26 '20 at 12:29
  • btw it is unlikely that the taks is to store `int`s in a `vector` – 463035818_is_not_an_ai May 26 '20 at 12:30
  • @idclev463035818: it would not even compile, no need to have more than 1 point :-) – Jarod42 May 26 '20 at 12:33
  • @Jarod42 oh right, I didnt want to complain too loudly ala "you didnt even test your code" because I thought they did... – 463035818_is_not_an_ai May 26 '20 at 12:35
  • if the question is not clear enough then it shouldn't be answered unless it is clarified. In any case code-only answers arent nice and even less nice when the code does not even compile – 463035818_is_not_an_ai May 26 '20 at 12:36
  • @idclev463035818 I do know that using #include in each case is not good. But for such small codes, it rarely matters coz there's rarely any chance for clashes. Everything that's said isnt applicable everywhere – Abhishek Bhagate May 26 '20 at 12:45
  • it does matter especially for such a small example, because there is absolutely no reason to write non-portable code for something that simple – 463035818_is_not_an_ai May 26 '20 at 12:49
  • "the one who asked question said - the function should return {0,1,2,3,4,5}" no they didn't, and no your code doesnt do that – 463035818_is_not_an_ai May 26 '20 at 12:50
  • @idclev463035818 It does do that. try adding another instance of object to myvectorA and running it. It will print all 6 values – Abhishek Bhagate May 26 '20 at 12:54
  • you removed the comment, it said "[...] the function should return {0,1,2,3,4,5} when myVectorA contains 6 elements?" When `myVectorA` contains 6 elements your implementation will return a vector with 18 elements and those elements are not `{0,1,2,3,4,5}` – 463035818_is_not_an_ai May 26 '20 at 12:56
  • but it doesn't matter at all, because the comment you refered to was mine trying to get clarification from OP. What OP really needs we still dont knw – 463035818_is_not_an_ai May 26 '20 at 12:58
  • Thank you Abishek. My vector `myVectorA` is a public class member and it needs to be returned from `vector get_A() const` function. But looking at the function prototype, it returns `vector` So my question is how to convert a vector of type `Structure A` to another vector of type `vector` – CppBeginner May 26 '20 at 15:54
  • @idclev463035818 Hi, As you pointed, Abhisheks solution works for me when my structure vector just contains one element. But the elements gets suffled when the original structure vector contains more than 1 elements. I verified it by printing my function return. Do you have a solution for this. Will be helpful. – CppBeginner May 26 '20 at 16:38
  • @CppBeginner your question is still not completely clear. There is no obvious way to convert a vector that contains signed integers to a vector that contains unsigned integers. You need to tell how that conversion should happen – 463035818_is_not_an_ai May 26 '20 at 16:39
  • @idclev463035818 I need to return my structure vector from the given function. But if i look at the function prototype, it shows the return parameter in the format `vector'. So i am not sure, how to do the conversion from a vector of type structure to `vector`. The solution given by Abhishek seems to be working when i just have 1 element for my structure vector, say myVectorA ({1,2,3}). But does not seem to be working when i have more than 1 structure element in my original vector. Say myVectorA ({1,2,3}, {4,5,6}, {7,8,9}). – CppBeginner May 26 '20 at 16:54
  • @idclev463035818 When i print the resultant vector `vector`, the resultant data seems to be suffled. Please let me know if there is any better solution for this. – CppBeginner May 26 '20 at 16:58
  • @CppBeginner does my solution not print according to the format I mentioned - [x0,y0,z0,x1,y1,z1,...] – Abhishek Bhagate May 26 '20 at 16:59
  • @CppBeginner Check the edit I made where I added three structures to myVectorA. So now it is myVectorA - [{1,2,3}, {4,5,6}, {7,8,9}]. This prints 1,2,3,4,5,6,7,8,9 as expected. – Abhishek Bhagate May 26 '20 at 17:08