vector<vector<double>> v; vector< vector<double> > v; class String {
public:
String(char* pp) :sz(strlen(pp)), p(new char[sz+1]) { strcpy(p,pp); }
~String() { delete[] p; }
char& Operator[](int i) { return p[i]; }
PRivate:
int sz;
char* p;
};
void f(char* x)
{
String s1(x);
String s2 = s1;
} template<class In, class Pred>
In find_if(In first, In last, Pred pred)
{
while (first!=last && !pred(*first)) ++first;
return first;
} find_if(1,5,3.14); // 錯誤template<class T> strUCt Forward_iterator {
static void constraints(T a) {
++a; a++; // 可以增加
T b = a; b = a; // 可以復制
*b = *a; // 可以廢棄和復制結(jié)果
}
Forward_iterator() { void (*p)(T) = constraints; }
}; template<class In, class Pred>
In find_if(In first, In last, Pred pred)
{
Forward_iterator<In>(); // 檢查模板參數(shù)類型
while (first!=last && !pred(*first)) ++first;
return first;
} template<Forward_iterator In, Predicate Pred>
In find_if(In first, In last, Pred pred); int x = find_if(1,2,Less_than<int>(7)); template<Value_type T,
Forward_iterator<T> In, // 迭代子在T序列中
Predicate<bool,T> Pred> // 帶有 T 參數(shù)并返回一個布爾值
In find_if(In first, In last, Pred pred); template<Forward_iterator In, Predicate Pred>
where (assignable<In::value_type, Pred::argument_type>)
In find_if(In first, In last, Pred pred); template <class T> concept Forward_iterator {
// 參數(shù)化的概念
Forward_iterator a;
++a; a++; // 可以增加
Forward_iterator b = a; b = a; // 可以復制
*b = *a; // 可以廢除和復制結(jié)果
T x = *a; *a = x; // 可以認為結(jié)果是T類型的
}; concept Forward_iterator { // 概念沒有用參數(shù)表示
Forward_iterator a;
++a; a++; //可以增加
Forward_iterator b = a; b = a; //可以復制
*b = *a; // 可以廢除和復制結(jié)果
}; int x = find_if(1,2,Less_than<int>(7)); void f(vector<int>& v, int* p, int n)
{
vector<int>::iterator q = find_if(v.begin(),v.end(),Less_than<int>(7));
int* q2 = find_if(p,p+n,Less_than<int>(7));
// …
} double vd[ ] = { 1.2, 2.3, 3.4, 4.5, 5.6 };
vector<double> v(vd, vd+5); vector<double> v;
v.push_back(1.2);
v.push_back(2.3);
v.push_back(3.4);
v.push_back(4.5);
v.push_back(5.6); vector<double> v = { 1.2, 2.3, 3.4, 4.5, 5.6 }; vector<double> v ({ 1.2, 2.3, 3.4, 4.5, 5.6 }); void f(const vector<double>& r);
// …
f({ 1.2, 2.3, 3.4, 4.5, 5.6 }); 新聞熱點
疑難解答