tags:

views:

498

answers:

5
Q: 

C++ Vector

Look this code(and forgive the miss of knowlegde).It outputs errors that I couldnot solve.I need to declare a vector of elements of struct C,but I need the number of elements be i(a input of type int).I also tried others aproachs but in all of them I recieved an error(cannot convert C to int,etc).How can I do this?

# include < iostream >
using std::cout;
using std::cin;
using std::endl;

# include < vector >
using std::vector;

struct C{
    int cor;
    vector<int>cores;

    };

    void LerVector( vector< C> &array ) ;

int main ()
{
     int n;
    bool done=false;
     bool don=false;
    vector<C>cidade;
    int i;


    while(!done){
    cout<<"Entre o número de cidades "<<endl;
    cin>>n;
    if(n>500)
    {
     cout<<endl;
     cout<<"O número máximo é 500"<<endl;
}
else
done=true;
}
n--;
while(!don){
cout<<"Entre o número de confederações"<<endl;
cin>>i;
if(i>100){
cout<<endl;
cout<<"Número máximo de 100 cidades"<<endl;

}
else {

 LerVector(  cidade) ;

don=true;
}
}


    cin.get();
    return 0;
}
//resolve...
 void LerVector( vector< C> &array ) 
  { 
    for ( size_t i = 0; i < array.size(); i++ ) 
      cin>>array[i];

  } // end function inputVector
A: 

Hi, if I guess what you want to do, it should be like this:

// First create an empty vector of C's
vector<C> cidade;

// cidade has zero elements now
// Read i from user
cin >> i;

// Resize vector to contain i elements
cidade.resize(i);

// Then go on and fill them.
int n;
for (n = 0; n < i; i++) {
  cin >> cores;
  cidade[n].cores.resize(cores);
  // now cidade[n].cores has 'cores' elements, but they are uninitialized
}
antti.huima
+1  A: 

Which errors did your code generate?

I'm also not sure what your code is supposed to do. In main(), you create a vector of C. But C also contains a vector of int's. Is that intended?

jalf
+1  A: 

I'm not really clear what you're trying to do.

However, I can already see one potential error in our code:

In LerVector, you come in with a reference to a vector that does not currently have any items in it, and therefore has a size of 0.

What you're trying to do is that as long as i is smaller than the size, you update that item in the array. However, when you start out size is 0 so I don't think you'll even go into the input loop.

Now, even if you did, since the vector is not initialized with any size, you may get an error that you're going out of bounds. You have to resize the rray.

Uri
A: 

One of the std::vector<T> constructors will take an initial size, and if declared after that number is know you can pass it to the constructor.

cin >> n;
std::vector<C> cidade(n);

Or you can use the resize method to change a vector's size.

Or you can use the add method to extend the vector (without explicitly giving a size).

But overall, it might be easier to give help with a full version of the code and more details of what the code is trying to do.

Richard
+3  A: 

Let's try with an explanation :)

cin >> array[i];

That tries to extract from cin into an object of struct C. Well, so it needs an operator>> that actually does that work:

istream & operator>>(istream &is, C &c) {
    is >> c.cor; // or into whatever member 
    return is;
}

In addition, as another one mentioned, you have to actually add the elements to the vector first:

while(!don){
    cout<<"Entre o número de confederações"<<endl;
    ....
} else {
    cidade.resize(i); // resize to i elements
    LerVector(cidade);
    don = true;
}

For the next time, please format the text (correct indent it). It was hard for me to step through it :)

Johannes Schaub - litb