问一下c++中vector<int> 和vector<int>::iterator有什么不同 引用问题 vector初始化

verctor是声明向量容器;
例如 verctor v,就是创建了一个名字叫v的向量容器。
vector::iterator是定义向量迭代器
例如,vector::iterator it 就可以
for(it=v.begin();it!=v.end();it++)
cout<<*it<<endl;
就把里面的内容都输出了

260. Single Number III

Medium

901

79

Favorite

Share
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

Example:

Input: [1,2,1,3,2,5]
Output: [3,5]

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        vector<int> re(2,0);
        int sum = 0;
      
        
         for (auto& t: nums) sum ^= t;
        
        int flag = sum&(~(sum-1));
        
        for(auto& t:nums) {
            if((t &flag) ==0) re[0]^= t;
            else re[1]^= t;
        }
        
        return re;
        
    }
};

注意引用的写法
还有vector 初始化写的那个(2,0)
不然会报错 Line 922: Char 34: runtime error: reference binding to null pointer of type ‘value_type’ (stl_vector.h)

产生原因:

1.对于一些stl和一些数据结构掌握不准确。
2.忽视判断条件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值