Archived

This topic is now archived and is closed to further replies.

pacrugby

vector and sscanf

Recommended Posts

can I directly do this somehow?? ie sscanf("%f",vect1.push_back()); ?? at the moment I have to use sscanf("%f",&x); vect1.push_back(x); TIA

Share this post


Link to post
Share on other sites
  
// C code:

float f[20]; // possibly uninitialized floats

sscanf ("%f", &f[10]); // scan into the 11th element


// STL code:

vector<float> f (20); // initializes vector to 0.0

sscanf ("%f", &f[10]); // scan into the 11th element



Gotta love STL.

Share this post


Link to post
Share on other sites
operator[] already returns a reference
I think that it sould be

  
vector<float> f (20); // initializes vector to 0.0

sscanf ("%f", f[10]); // scan into the 11th element


Share this post


Link to post
Share on other sites
Well sscanf needs a pointer so it should probably be:
    
vector<float> f (20); // initializes vector to 0.0

sscanf ("%f", &(*f[10])); // scan into the 11th element


But I honestly have no idea if that will work.

Share this post


Link to post
Share on other sites
quote:
Original post by Scarab0
Well sscanf needs a pointer so it should probably be:

In many contexts there is no effective difference between references and pointers. Further more, the fact that a function takes a pointer as an argument doesn''t mean you must pass it an argument; it means the function will take the address of whatever argument you pass to it (with type restrictions). ie

int func( int *pInteger );
int i = 255;
func(&i); // note need to get address-of i using operator &.
int j[25];
// initialize j if necessary
func(j); // still valid; func() takes address of j[].
int *k;
k = new int[NUM_INTS]; // assume NUM_INTS to be a constant
func(k); // same as func(j), only array is dynamic. func() still takes address of k[].

Share this post


Link to post
Share on other sites
Are you listening (reading)? You can''t do it in one statement using push_back(); get over it! If your vector is already allocated (using resize(), for example), you can assign the value returned by sscanf() to the ith element. That''s it.

Share this post


Link to post
Share on other sites
Just for the record, the proper sscanf statement is

  
sscanf("%f", &f[10]);


pacrugby:
What you''re trying to do is impossible. Period. The others have made very good explanations why.

Share this post


Link to post
Share on other sites