STL模板大全(第一弹)

首先推荐一个网址“http://blog.jobbole.com/83461/”,其中详细记载了C++的模板元编程

#include<iostream>
using namespace std;

struct Point{
    int x,y;
    Point(int x=0,int y=0):x(x),y(y){}
};

Point operator + (const Point &A,const Point &B){
    return Point(A.x+B.x,A.y,B.y);
}

ostream& operator << (ostream &out,const Point &p){
    out<<"("<<p.x<<","<<p.y<<")";
    return out;
}//ostream表示输出流。。。
//其中的point表示该类型
//ostream表示的是cout这一操作
//该操作为重载运算符操作
 

template<typename T>
T sum(T *begi,T *en){
    T *p=begi;
    T ans=0;
    for (T *p=begin; p!=end; p++)
        ans=ans+*p;
    return ans;
}//紫书模板P106
#include<iostream>
#include<string>
#include<set>
#include<sstream>
using namespace std;

struct str{
    string s;
    bool operator<(const str &s1) const{
        return s.length()<s1.s.length();
    }
};

set<str> dict;

string s,s2;
int main(){
    while (cin>>s){
        for (int i=0; s[i]; i++)
        if (isalpha(s[i])) s[i]=tolower(s[i]);else s[i]=' ';
        stringstream ss(s);
        while (ss>>s2) dict.insert((str){s2});
    }
        for (set<str>::iterator i=dict.begin(); i!=dict.end(); i++)
            cout<<*i<<endl; 
    return 0;
}//CE代码,打算向JS2XXX大佬请教
#include<iostream>
#include<string>
#include<set>
#include<sstream>
using namespace std;

set<string> dict;

string s,s2;
int main(){
    while (cin>>s){
        for (int i=0; s[i]; i++)
        if (isalpha(s[i])) s[i]=tolower(s[i]);else s[i]=' ';
        stringstream ss(s);
        while (ss>>s2) dict.insert(s2);
    }
        for (set<string>::iterator i=dict.begin(); i!=dict.end(); i++)
            cout<<*i<<endl; 
    return 0;
}

 

#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;

map<string,int> cnt;
vector<string> words;
vector<string> ans;
string s;

inline string rep(string s){
    string ans=s;
    for (int i=0; ans[i]; i++) ans[i]=tolower(ans[i]);
    sort(ans.begin(),ans.end());
    return ans;
}

int main(){
    while (cin>>s){
        if (s[0]=='#') break;
        words.push_back(s);
        string r=rep(s);
        if (!cnt.count(r)) cnt[r]=0;
        cnt[r]++;
    }
    for (int i=0; i<words.size(); ++i)
        if (cnt[rep(words[i])]==1) ans.push_back(words[i]);
    sort(ans.begin(),ans.end());
    for (int i=0; i<ans.size(); ++i)
        cout<<ans[i]<<endl;
    return 0;
}

 

posted @ 2017-10-18 10:20 最弱的蒟蒻 阅读(...) 评论(...) 编辑 收藏