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; // 可以废弃和复制结果
}
Forward_iterator() { void (*p)(T) = constraints; }
};
template<class In, class Pred>
In find_if(In first, In last, Pred pred)
{
Forward_iterator<In>(); // 检查模板参数类型
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 参数并返回一个布尔值
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 {
// 参数化的概念
Forward_iterator a;
++a; a++; // 可以增加
Forward_iterator b = a; b = a; // 可以复制
*b = *a; // 可以废除和复制结果
T x = *a; *a = x; // 可以认为结果是T类型的
};
concept Forward_iterator { // 概念没有用参数表示
Forward_iterator a;
++a; a++; //可以增加
Forward_iterator b = a; b = a; //可以复制
*b = *a; // 可以废除和复制结果
};
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 });
新闻热点
疑难解答