I want to run the following code. Its giving error as "Fatal Error" not able to open 'print.hpp' . No such file or directory.. I didn't understand what is #include "print.hpp". what type of file it is. shall we have to write this header file before executing the program.

// stl/transform1.cpp

   #include <iostream>
   #include <vector>
   #include <set>
   #include <algorithm>
   #include "print.hpp"
   #include<conio.h>

   int square (int value)
   {
       return value*value;
   }

   int main() 
   {
       std::set<int> coll1;
       std::vector<int> coll2;

       //insert elements from 1 to 9 into coll1
       for (int i=1; i<=9; ++i) {
           coll1.insert(i);
       }
       PRINT_ELEMENTS(coll1,"initialized: ");

       //transform each element from coll1 to coll2
       // - square transformed values
       std::transform (coll1.begin(),coll1.end(),     //source
                       std::back_inserter(coll2),     //destination
                       square);                       //operation

       PRINT_ELEMENTS(coll2,"squared:        ");
   getch();
   }

Recommended Answers

All 6 Replies

Let me guess: This came out of: The C++ Standard Library - A Tutorial and Reference?

if yes:
create a file "print.hpp" and wack this code in it:

#include <iostream>

template <class T>
inline void print_elements (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << ' ';
    }
    std::cout << std::endl;
}

Or just throw the code in your cpp file and delete the #include "print.hpp" Also loose the getch() . If you want to play with c++ use cin.get() instead. That way you can also delete the line #include <conio.h> . The conio header isn't standard c++

>> shall we have to write this header file before executing the program.
Yes, or get it from whereever you got that *.cpp file. The file you posted is not part of STL because STL doesn't contain main().

Let me guess: This came out of: The C++ Standard Library - A Tutorial and Reference?

if yes:
create a file "print.hpp" and wack this code in it:

#include <iostream>

template <class T>
inline void print_elements (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << ' ';
    }
    std::cout << std::endl;
}

Or just throw the code in your cpp file and delete the #include "print.hpp" Also loose the getch() . If you want to play with c++ use cin.get() instead. That way you can also delete the line #include <conio.h> . The conio header isn't standard c++

Thanks for quick reply. I got output..

Let me guess: This came out of: The C++ Standard Library - A Tutorial and Reference?

if yes:
create a file "print.hpp" and wack this code in it:

#include <iostream>

template <class T>
inline void print_elements (const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    std::cout << optcstr;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {
        std::cout << *pos << ' ';
    }
    std::cout << std::endl;
}

Or just throw the code in your cpp file and delete the #include "print.hpp" Also loose the getch() . If you want to play with c++ use cin.get() instead. That way you can also delete the line #include <conio.h> . The conio header isn't standard c++

yes i am reading The C++ Standard Library - A Tutorial and Reference. I want to the program on aritmetic/relational/logical (etc) operations using STLs. In the above said book Which topics will help me to do these programs. I completed 5th chapter in this book. See attach for complete program..

Did you read the link I posted in your other thread?

The attachment looks to me like a homework assignment, so you'll have to try it yourself. Come back when you have a specific problem with a piece of code.

[edit]
And as my signature says: DO NOT PM me for help.

Did you read the link I posted in your other thread?

The attachment looks to me like a homework assignment, so you'll have to try it yourself. Come back when you have a specific problem with a piece of code.

[edit]
And as my signature says: DO NOT PM me for help.

I am very very sorry. I have not yet read link which you posted in previous thread. I am reading c++ The standard library A tutorial and reference. After completing this tutorial i will go to read that link. and will get back to you if any problem occurs in my code.
I am very much thankful to you for your guideness

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.