A
arnuld
it does not run and even does not even give me any clue to the problem in
its output
/* C++ Primer - 4/e
* chapter 5 - Expressions
* exercise 5.18
* STATEMENT
* write a programme that defines a vector of pointers to strings.
read the vector, printing each string and its corresponding size.
*/
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string*> psvec;
std::string input_string;
std::string* ps;
while(std::cin >> input_string)
{
*ps = input_string;
psvec.push_back(ps++);
}
/* printing the strings pointed by the pointers in the vector*/
for(std::vector<std::string*>::const_iterator iter = psvec.begin();
iter != psvec.end(); ++iter)
{
std::cout << "string: " << **iter
<< " size: " << (*iter).size() /* error is here */
<< std::endl;
/* double-defrenced operator because it points to a pointer
rather than a value */
}
/* i was thinking of using "std::copy" from <algorithm> and
"ostream_iterator" from <sstream> from but was not able to
understand the peculiar mechanism of both of them */
return 0;
}
/* OUTPUT
/home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
ex_05-18.cpp ex_05-18.cpp: In function ‘int main()’: ex_05-18.cpp:32:
error: request for member ‘size’ in ‘iter.
__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with
_Iterator = std::string* const*, _Container = std::vector<std::string*,
std::allocator<std::string*> >]()’, which is of non-class type
‘std::string* const’
/home/arnuld/programming/cpp $
*/
its output
/* C++ Primer - 4/e
* chapter 5 - Expressions
* exercise 5.18
* STATEMENT
* write a programme that defines a vector of pointers to strings.
read the vector, printing each string and its corresponding size.
*/
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string*> psvec;
std::string input_string;
std::string* ps;
while(std::cin >> input_string)
{
*ps = input_string;
psvec.push_back(ps++);
}
/* printing the strings pointed by the pointers in the vector*/
for(std::vector<std::string*>::const_iterator iter = psvec.begin();
iter != psvec.end(); ++iter)
{
std::cout << "string: " << **iter
<< " size: " << (*iter).size() /* error is here */
<< std::endl;
/* double-defrenced operator because it points to a pointer
rather than a value */
}
/* i was thinking of using "std::copy" from <algorithm> and
"ostream_iterator" from <sstream> from but was not able to
understand the peculiar mechanism of both of them */
return 0;
}
/* OUTPUT
/home/arnuld/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
ex_05-18.cpp ex_05-18.cpp: In function ‘int main()’: ex_05-18.cpp:32:
error: request for member ‘size’ in ‘iter.
__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with
_Iterator = std::string* const*, _Container = std::vector<std::string*,
std::allocator<std::string*> >]()’, which is of non-class type
‘std::string* const’
/home/arnuld/programming/cpp $
*/