cplusplus

C++/STL学习

上一篇 / 下一篇  2011-08-14 23:42:15 / 个人分类:C++

#include <stdio.h>
template <typename T>
T add(T t1, T t2)
{
    return t1 + t2;
}

class MyString
{
public:
    char str1[256];

    MyString(char *str)
    {
        strcpy(str1, str);
    }
    
    friend MyString operator+(MyString &str1, MyString& str2)
    {
        strcat(str1.str1, str2.str1);
        return str1;
    }

    MyString operator+=(MyString& str2)
    {
        strcat(str1, str2.str1);
        return *this;
    }
};


int main (void)
{
//    cout<< add(5.2, (double)4) << endl;

    MyString s1("haha"), s2("hoho");
//    s1 + s2;
    s1 += s2;
    printf("%s\n", s1.str1);
    return 0;
}
 
#include <iostream>  
#include <algorithm>  
#include <vector>  
#include <string>  
using namespace std;
int myfind()
{
 /*
#define SIZE 100
 int iarray[SIZE];
 iarray[20] = 50;
 int* ip = find(iarray, iarray + SIZE, 50);
 if (ip == iarray + SIZE)
  cout << "50 not found in array" << endl;
 else
  cout << *ip << " found in array" << endl;
 return 0;
 */
 vector<int> intVector(100);
 intVector[20] = 50;
 vector<int>::iterator intIter =
  find(intVector.begin(), intVector.end(), 50);
 if (intIter != intVector.end())
  cout << "Vector contains value " << *intIter << endl;
 else
  cout << "Vector does not contain 50" << endl;
 return 0;
}
long print(long d)
{
 cout << d << ' ';
 return 0;
}
long mysort()
{
 vector<long> vec;
 vec.push_back(10);
 vec.push_back(5);
 vec.push_back(11);
 vec.push_back(6);
 long si = vec.size();
 vector<long>::iterator It;
// sort()
// sort(vec.begin(), vec.end());
 for (int i = 0; i < vec.size(); vec.front(), i++)
 {
  printf("%d\t", vec.at(i));
 }
 cout << endl;
 sort(vec.begin(), vec.end());
// sort(It.begin(), It.end());
 for_each(vec.begin(), vec.end(), print);
  cout << endl;
 printf("%d\n", si);
 return 0;
}  
int main ()  
{
 mysort();
 myfind();
 return 0;
    //第一种运用方法:定义一个int类型的迭代器  
    istream_iterator<int> start_cin(cin);  
    istream_iterator<int> end_of_cin;  
    vector<int> v;   
 
    cout << "Enter sequance of integers " 
        << "(d - quit) : ";  
    copy(start_cin,end_of_cin,back_inserter(v));   
 
    for ( vector<int>::iterator It = v.begin();  
 It != v.end(); It++ )  
        cout << *It << " - ";  
    cout << endl;  
 
    //第二种方法:定义一种string类型的迭代器  
    vector<string> vS;  
    cout << "Enter three strings : ";  
    for ( int i = 0; i < 3; i++ )  
        vS.push_back(*istream_iterator<string>(cin));  
    ostream_iterator<string> sIt(cout,", ");  
    copy(vS.begin(),vS.end(),sIt);  
    cout << endl;   
 
    return 0;  
}  
 
http://net.pku.edu.cn/~yhf/UsingSTL.htm
http://topic.csdn.net/t/20050512/22/4002702.html 

TAG:

 

评分:0

我来说两句

Open Toolbar