int a[10];
int b[10] = {1,1,2,2,3,3,4,4,5,5};
for(int i=0;i<10;i++){
a[i] = b[i];
}
但是需要数组a里面的元素不重复也就是说当b的第二个元素插入的时候需要判断一个b[1]是否已经在a里面了 这个判断怎么写啊 记得有一个是否包含的判断的 具体怎么整啊
伊谢尔伦2017-04-17 14:22:08
You can use std::find
, like this
vector<int> a(10);
vector<int> b = {1,1,2,2,3,3,4,4,5,5};
for(auto i : b)
{
auto res = std::find(a.begin(), a.end(), i);
if(res == a.end())
a.push_back(i);
}
ringa_lee2017-04-17 14:22:08
std::set<int>
If you use an array, you can traverse whether the element already exists every time you insert it.
PHP中文网2017-04-17 14:22:08
b can be sorted
b is ordered
Just judge whether the previous element is equal to the element currently to be inserted
if ( a的最后一个元素 != 当前要插入的元素) {
插入到a的最后
}
b is unordered
can be sorted and then execute the above algorithm
b cannot be sorted and is unordered
The order of a and b does not have to be consistent
Use special means (such as the process of heap creation) to insert into a
a and b are in the same order
You can only check whether you want the same elements before inserting or use an auxiliary data structure to record the inserted elements
Of course you can use the ready-made set in stl
or other things such as bit arrays. Of course, if you are learning, it is better to implement it once