pair STL query

O

onkar

#include<iostream>
using namespace std;
int main(void){
const pair<const char*,const char*> arr[]={
pair<const char*,const char*>("1","1"),
pair<const char*,const char*>("12","12"),
pair<const char*,const char*>("123","123"),
pair<const char*,const char*>("1234","1234"),
pair<const char*,const char*>("12345","12345"),
};
cout<<sizeof(char)<<endl;
cout<<sizeof(arr[0])<<endl;
cout<<sizeof arr<<endl;
cout<<sizeof(arr)/sizeof(arr[0])<<endl;
return 0;
}


gives me :

1
8 <- ????? Why ????
40
5
 
I

Ian Collins

onkar said:
#include<iostream>
using namespace std;
int main(void){
const pair<const char*,const char*> arr[]={
pair<const char*,const char*>("1","1"),
pair<const char*,const char*>("12","12"),
pair<const char*,const char*>("123","123"),
pair<const char*,const char*>("1234","1234"),
pair<const char*,const char*>("12345","12345"),
};
cout<<sizeof(char)<<endl;
cout<<sizeof(arr[0])<<endl;
cout<<sizeof arr<<endl;
cout<<sizeof(arr)/sizeof(arr[0])<<endl;
return 0;
}


gives me :

1
8 <- ????? Why ????

2*sizeof(char*) ?
 
R

Rolf Magnus

onkar said:
#include<iostream>
using namespace std;
int main(void){
const pair<const char*,const char*> arr[]={
pair<const char*,const char*>("1","1"),
pair<const char*,const char*>("12","12"),
pair<const char*,const char*>("123","123"),
pair<const char*,const char*>("1234","1234"),
pair<const char*,const char*>("12345","12345"),

Have a look at std::make_pair.
};
cout<<sizeof(char)<<endl;
cout<<sizeof(arr[0])<<endl;
cout<<sizeof arr<<endl;
cout<<sizeof(arr)/sizeof(arr[0])<<endl;
return 0;
}


gives me :

1
8 <- ????? Why ????

Why not? What did you expect?
 
S

sonison.james

Hi,

sizeof of STL containers does not return the size of the contained
object - sizeof vector<char> containg 100 chars is not 100. You need to
check your STL implementation of pair to see why it is returning 8.


Thanks and regards
Sonison James
 
R

Richard Herring

In message <[email protected]>,
(e-mail address removed) writes

[Please don't top-post. Rearranged]
onkar said:
#include<iostream>
using namespace std;
int main(void){
const pair<const char*,const char*> arr[]={
pair<const char*,const char*>("1","1"),
pair<const char*,const char*>("12","12"),
pair<const char*,const char*>("123","123"),
pair<const char*,const char*>("1234","1234"),
pair<const char*,const char*>("12345","12345"),
};
cout<<sizeof(char)<<endl;
cout<<sizeof(arr[0])<<endl;
cout<<sizeof arr<<endl;
cout<<sizeof(arr)/sizeof(arr[0])<<endl;
return 0;
}


gives me :

1
8 <- ????? Why ????
40
5


sizeof of STL containers does not return the size of the contained
object - sizeof vector<char> containg 100 chars is not 100.

std::pair isn't a "container" the way vector is. It's just a templated
2-element structure.
You need to
check your STL implementation of pair to see why it is returning 8.

What would you expect the size of the following to be?

struct {
const char * first;
const char * second;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top