i have a problem when i read c++ primer ,i don't know what the
follow codes meaing.
char *word[]={"frank","english","edali","slina"};
It means that the author of the code is using a deprecated
feature, which good programmers have avoided for many years now.
That should be:
char const* words[]
= { "frank", "english", "edali", "slina" } ;
Probably static, as well, and perhaps the pointers should be
const too:
static char const* const words[] = ...
This declares an array of four pointers to a char; for
historical reasons, they can either point to a single char,
or the first char in an array of char. In this case, your
initialization initializes them to point to four anonymous
arrays of char const (which is why "char const*" is necessary,
rather than simply "char*"). Each of these arrays is
initialized with a '\0' terminates sequence of characters,
specified by the string literals.
The array has four elements because that's how many initializers
you gave.
size_t word_size=sizeof(word)/sizeof(char*);
This is an error prone and very out of date way of determining
the number of elements in the array. Today, you'd almost
certainly not use it---far better alternatives exist.
list<string> word2(word,word+word_size);
This calles the two iterator constructor of list<string>, which
uses the values between the begin and the end iterator to
initialize the list.
Today, the idiomatic way of writing this code would be:
static char const* const words[]
= { "frank", "english", "edali", "slina" } ;
std::list< std::string > words2( begin( words ), end( words ) ) ;
Where you've got some library header which defines begin and end
appropriately:
template< typename T, std::size_t N >
T*
begin( T (&array)[ N ] )
{
return array ;
}
template< typename T, std::size_t N >
T*
end( T (&array)[ N ] )
{
return array + N ;
}
(And no, you're not expected to come up with these functions on
your own. Or even really understand them if you're just
starting with C++. But whatever text you're using to teach you
the language should provide them, at least if it bothers to
present C style arrays at all.)
and then what's the contents of list? why the word_size=sizeof(word)/
sizeof(char*)?
Because sizeof returns the number of bytes in an object, but
pointer arithmetic works with the number of elements. Yet
another broken feature we've inherited from C.