ambarisha.kn 15 Junior Poster in Training

Container V1: 1 2 3 4 5 6 7

I want to print the elements which are greater than equal to 3 but less than 6. How to do this using Standard template library functions..

We have logical_and, greater_equal and less_equal Predefined Function Objects. But i dont know how to implement this. Could any one please solve this problem

ambarisha.kn 15 Junior Poster in Training

yes i studied those tutorials. Actually i am working on japanese language. Now i got some examples which was in japanese page. I will study these examples. It may helpful for me.

ambarisha.kn 15 Junior Poster in Training

yes. But using STLs.

ambarisha.kn 15 Junior Poster in Training

I am not getting with those examples. It looks difficukt to understand and there are no example programs. They have used iota functions which is not working in my system even if i include #include<numeric>. I completed arithmetic operations, relational operations from "C++ standard library A tutorail and reference". But ia m not getting good tutorials for logic operations. Would u please get me some more understandable tutorials with examples

ambarisha.kn 15 Junior Poster in Training

Hi,

I need some algorithms with syntax to work out logical operations using Standard template library(STLs). Please get me some examples of these or teach me regarding logical operations using STLs.

ambarisha.kn 15 Junior Poster in Training

Replace them with what? You're missing the last parameter for the replace_if() function. If you want to replace them with '1' for example, you should use replace_if(V2.begin(),V2.end(), bind2nd(less<int>() ,5),1); and did you remember to

#include <algorithm>
#include <functional>

?

Thanks , I got it. I was missed last parameter.

replace_if(V2.begin(),V2.end(),               //range
                   bind2nd(less<int>() ,5),0);          //replace criterion

Now its working.

ambarisha.kn 15 Junior Poster in Training

Hi,

I want to replace the numbers which are less than 5 in a container. So i write a code as follows. But it is not working. Please get me the solution for this. Destination container should be V2 itself. Where we have to specify destination container.

replace_if(V2.begin(),V2.end(),               //range
                   bind2nd(less<int>() ,5));          //replace criterion
ambarisha.kn 15 Junior Poster in Training
#include<iostream>
#include<vector>
#include<deque>
#include<list>
#include<algorithm>
#include<functional>
#include<conio.h>
using namespace std;
int main()
{
	vector<int> V1;				 //vector container for integer elements
	deque<int> V2;				 //dequeu container for integer elements
	list<int> V3;				 //list container for integer elements
  
	vector<int>::iterator pos1;		// declaration of iterator
	deque<int>::iterator pos2;		// declaration of iterator		
	list<int>::iterator pos3;		// declaration of iterator
    

	//append elements from 12 to 18 in Vector container V1
	for(int i=12;i<=18;++i)
	{
		  V1.push_back(i);			//inserts an element at the back of the collection
	}

	/*print all elements of V1
     * - iterate over all elements    */
	cout<<"V1:       ";
	for(pos1=V1.begin();pos1!=V1.end();++pos1)
	{
		  cout<<*pos1<<' ';
	}
	cout<<endl;

	
	//append elements from 2 to 8 in deque container V2
	for(int i=2;i<=8;++i)
	{
		  V2.push_front(i);			//inserts an element at the front of the collection
	}
	
	/*print all elements of V2
     * - iterate over all elements    */
	cout<<"V2:       ";
	for(pos2=V2.begin();pos2!=V2.end();++pos2)
	{
		  cout<<*pos2<<' '<<' ';
	}

	cout<<endl;
	int ch;
	do
	{
	cout<<"Select your option\n1.Add\n2.Subtract"<<endl;
	cin>>ch;
	switch(ch)
	{
	case 1: 
			//transform  resulted element to list V3 after addition of each element in vector V1 and deque V2
			 // - Add transformed values
			transform(V1.begin(), V1.end(),				//First sorce range
									V2.begin(),				//Second source range
									back_inserter(V3),		//destination 
									plus<int>());			//operation
				

			//dispaly the resulted output
			cout<<"V1+V2:"<<endl
				;
			for(pos3=V3.begin();pos3!=V3.end();++pos3)
			{	
				cout<<*pos3<<' ';
			}
			/*V3.~list<int>();*/
			V3.clear();
			cout<<endl;
			break;
	case 2: 
			//transform  resulted element to list V3 after addition of each element in vector V1 and deque V2
			// - Add transformed values
			transform(V1.begin(), V1.end(),				//First sorce range
								V2.begin(),				//Second source range
								back_inserter(V3),		//destination 
								minus<int>());			//operation
			

			/* transform (V1.begin(),V1.end(),
										 back_inserter(V3),
										bind2nd (minus<int>(),5)
										);*/
			//dispaly the …
ambarisha.kn 15 Junior Poster in Training

i am getting error in following program. what is "iota". Why it is giving an error in the following program

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
  vector<int> V(10);

  iota(V.begin(), V.end(), 7);
  copy(V.begin(), V.end(), ostream_iterator<int>(cout, " "));
  cout << endl; 
  cin.get();
}
ambarisha.kn 15 Junior Poster in Training

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

ambarisha.kn 15 Junior Poster in Training

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..

ambarisha.kn 15 Junior Poster in Training

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..

ambarisha.kn 15 Junior Poster in Training

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();
   }
ambarisha.kn 15 Junior Poster in Training

why we have to use following code in STL programs. why it is needed.

use namespace std;
ambarisha.kn 15 Junior Poster in Training

Hi,

How to do arithmetic and relational operations using Standard Template Library.(STLs).
Could any one please guide me regarding this. I am new to Standard Template Library.

ambarisha.kn 15 Junior Poster in Training

Hi,

I dont know much about Standard template library(Vectors, Lists, Deque).
I want to do one Program which is as follows.

Write a program to create a data bank which holds the operator and operands of arithmatic/relational/bitwise(etc.,) operations. (Use STLs)
The no. of operand fields(input and output) is variable depending on the kind of operation.

Details:
The number input and ouput fields is unpredictable(ie. it may vary depending on the “operator” field).
For eg.,
AND operation takes 2 inputs and one output( a = b & c),
but
NOT operation has one input and one ouput ( x = !y).

Dev. Platform:
Visual Studio 6.0(if possible)
Visual C++

Note:
1) The number of fields may increase or decrease dynamically.
2) There is no limit upto how many fields it may increase.

Could any one please help me to do this. I dont have idea about standard template libray. sp please get me some tutorials or get me the code for this program