1、大致有以下几种方法实现用于把一个vector赋值给另一个vector:
方法1:
1 | vector<int > v1(v2);//声明 |
方法2:使用swap进行赋值:
1 | vector<int > v1();v1.swap(v2);//将v2赋值给v1,此时v2变成了v1 |
方法3:使用函数assign进行赋值:
1 2 | vector<int > v1;//声明v1 v1.assign(v2.begin(), v2.end());//将v2赋值给v1 |
方法4:使用循环语句赋值,效率较差
1 2 3 4 | vector<int >::iterator it;//声明迭代器 for(it = v2.begin();it!=v2.end();++it){//遍历v2,赋值给v1 v1.push_back(it); }/
|
2、对非静态成员引用必须与特定对象相对 这句话 是什么意思!
意思就是引用非静态成员前应该先声明该类的对象。
比如类A这样定义
class A
{
private:
int n;
}
要使用n就要先这样声明A的对象,或者是使用class中的一个非静态成员函数,也必须这样使用。
A a;
a.n=1
3.注意变量在哪个{}里面定义,那么这个变量的作用域就在那个括号里面,在{}外面是不可见的。
4、记住对vector的输出只能是逐个输出,不能将整个vector进行输出。
t题目:
Write a program that outputs the string representation of numbers from 1 to n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
#ifndef _Solution_
#define _Solution_
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
vector<string> fizzBuzz(intn) {
vector<string>fizzBuzz1;
for (unsigned int ix = 1;ix <= n; ++ix)
if ((ix % 3)&& (ix % 5)){
string s =to_string(ix);
fizzBuzz1.push_back(s);
s = "";
}
else if ((!(ix %3))&& (ix % 5))
fizzBuzz1.push_back("Fizz");
else if (!(ix %5)&& (ix % 3))
fizzBuzz1.push_back("Buzz");
else
fizzBuzz1.push_back("FizzBuzz");
return fizzBuzz1;
}
};
#endif
int main() {
static int n = 15;
Solution a;
vector<string>vectemp(a.fizzBuzz(n)); //在这里定义一个中间vector
//接受另外一个class里面的vector
for (unsigned int ix = 0; ix <n;++ix) {
cout << vectemp[ix]<< endl;
}
}
5、记住operator<<的重载必须有一个是class的对象才能重载。
6、将int转化成string,在c11里面有函数to_string,可以进行转化。