⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string_sort.cpp

📁 我学习C++ Primer Plus过程中写下的课后作业的编程代码
💻 CPP
字号:
//把5个string字符串按首字母从低到高打印出来

#include <iostream>
#include <string>
using namespace std;

void selectionSort(string str[],int n);

int main()
{
	int i=0;
	string arr[5]={ "ebey","fool","about","cout","boy"};
	selectionSort(arr,5);
	for(i=0;i<5;i++)
	{
		cout<<arr[i]<<endl;
	}
	return 0;
}


void selectionSort(string str[],int n)		//选择排序
{
	string temp;
	int smallIndex;		//存储数组中最小值的下标
	int j;
	int pass;
	for(pass=0;pass<n-1;pass++)
	{
		smallIndex=pass;	
		for(j=pass+1;j<n;j++)
		{
			if(str[j]<str[smallIndex])
				smallIndex=j;		//找到比最小值小的值的下标
		}
		if(smallIndex!=pass)
		{
			swap(str[pass],str[smallIndex]);      //swap是strig类的一个成员函数
		}
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -