swap使用,奇偶排序

#include <iostream>
#include<ctime>
#include<vector>
#include<stack>
#include <forward_list>
#include<array>
using namespace std;
void fun(int a[], int len1, int b[], int len2)
{
	int i = len1 + len2;
	i--;
	len1--;
	len2--;
	while (i > len1 && len2 >= 0)
	{
		if (a[len1] >= b[len2])
		{
			a[i--] = a[len1--];
		}
		else
		{
			a[i--] = b[len2--];
		}
	}
	/*while (len1 >= 0)
	{
	a[i--] = a[len1--];
	}
	while (len2 >= 0)
	{
	a[i--] = b[len2--];
	}*/
}
void swp1(int* p, int* q)
{
	int* temp = p;
	p = q;
	q = temp;
}
void swp2(int*p, int* q)
{
	int temp = *p; 
	*p = *q;
	*q = temp;
}
void swp3(int* &p, int* &q)
{
	int* temp = p;
	p = q;
	q = temp;
}
int main1()
{
	int a = 5, b = 10;
	int* p = &a, *q = &b;
	cout << "交换前:" << endl;
	cout << p << " " << q << endl;
	cout << *p << " " << *q << endl;
	swp1(p, q);
	cout << "交换后:" << endl;
	cout << p << " " << q << endl;
	cout << *p << " " << *q << endl;

	int a2 = 5, b2 = 10;
	int* p2 = &a2, *q2 = &b2;
	cout << "交换前:" << endl;
	cout << p2 << " " << q2 << endl;
	cout << *p2 << " " << *q2 << endl;
	swp2(p2, q2);
	cout << "交换后:" << endl;
	cout << p2 << " " << q2 << endl;
	cout << *p2 << " " << *q2 << endl;

	int a3 = 5, b3 = 10;
	int* p3 = &a3, *q3 = &b3;
	cout << "交换前:" << endl;
	cout << p3 << " " << q3 << endl;
	cout << *p3 << " " << *q3 << endl;
	swp3(p3, q3);
	cout << "交换后:" << endl;
	cout << p3 << " " << q3 << endl;
	cout << *p3 << " " << *q3 << endl;
	return 0;
}

int main()
{
	vector<int> a = { 1, 2, 3, 4, 5, 6 };
	vector<int> b;
	cout << a.size() << " " << a.capacity() << endl;
	//forward_list<int> a;
	array<int, 6> c = { 1, 2, 3, 4, 5, 6 }; 
	//c.fill(4);
	
	for (auto ss:c)
	{
		cout << ss << " " ;
	}
	cout << c.size() << " "<< endl;
	cout << find(a.begin(), a.end(), 2) - a.begin() << endl;
	b.swap(a);
	cout << a.size()<< " "<< a.capacity()<<endl;
	for (int i = 0; i < b.size(); i++)
	{
		cout << b.at(i) << " ";
	}
	cout << endl;

	return 0;
}

void reorderOddEven(vector<int> &a)
{
	int n = a.size();
	for (int i = 0; i < n;i++)
	{
		if (a[i] & 1)
		{
			n--;
			int temp = a[i];
			for (int k = i; k < a.size()-1; k++)
			{
				a[k] = a[k + 1];
			}
			a[a.size()-1] = temp;
		}
	}
	
}

int main3()
{
	vector<int> a = { 1, 2, 3, 4, 5 };
	reorderOddEven(a);
	for (int i = 0; i < a.size(); i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;              //out:2,4,1,3,5



	return 0;
}

猜你喜欢

转载自blog.csdn.net/sinat_36412790/article/details/80416944