string_sort.cpp

来自「我学习C++ Primer Plus过程中写下的课后作业的编程代码」· C++ 代码 · 共 44 行

CPP
44
字号
//把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 + =
减小字号Ctrl + -
显示快捷键?