By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,911 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,911 IT Pros & Developers. It's quick & easy.

pair STL query

P: n/a
#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

Jul 8 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
onkar wrote:
#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*) ?

--
Ian Collins.
Jul 8 '06 #2

P: n/a
onkar wrote:
#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?
40
5
Jul 8 '06 #3

P: n/a
Hi,

sizeof of STL containers does not return the size of the contained
object - sizeof vector<charcontaing 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
onkar wrote:
#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
Jul 10 '06 #4

P: n/a
In message <11*********************@b28g2000cwb.googlegroups. com>,
so***********@gmail.com writes

[Please don't top-post. Rearranged]
>onkar wrote:
>#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<charcontaing 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;
};
--
Richard Herring
Jul 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.