468,467 Members | 2,671 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,467 developers. It's quick & easy.

pair STL query

#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
4 12016
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
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
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
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.

Similar topics

14 posts views Thread by Neil Zanella | last post: by
3 posts views Thread by JustSomeGuy | last post: by
6 posts views Thread by pmatos | last post: by
1 post views Thread by MDS | last post: by
4 posts views Thread by Florent Garcin | last post: by
11 posts views Thread by kietzi | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.