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.忽视判断条件