leetcode412+vector赋值+非静态成员引用必须与特定对象相对+vector的输出+operator<<+to_string

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,可以进行转化。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值