注册 登录
编程论坛 C++教室

这个程序哪有错,求解答

蜗牛cr 发布于 2015-03-13 23:55, 712 次点击
#include<iostream>
#include<string>
using namespace std;
int main()
{
  int i,j;
  string str[5]={"we","go","to","school","today"};
  string t;
  for(i=0;i<5;i++)
    for(j=0;j<5-i;j++)
    {
     if(str[j]>str[j+1])
     {
      t=str[j];
      str[j]=str[j+1];
      str[j+1]=t;
     }
    }

  for(i=0;i<5;i++)
    cout<<str[i]<<" ";
  cout<<endl;
  return 0;
}
10 回复
#2
zklhp2015-03-14 00:03
这是C++ 需要我帮你转到C++版块么
#3
zklhp2015-03-14 00:23
程序代码:

// g++ -Wall -fomit-frame-pointer -funroll-loops -Ofast -march=corei7-avx -msse4.2 -mavx -std=c++11 a.cpp -lm -o a
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::sort;

int main(void)
{
    vector<string> content = {"we", "go", "to", "school", "today"};
    sort(content.begin(), content.end());
    for (auto output : content)
        cout << output << endl;
   
    return 0;
}


我口味比较重 C++11 献丑了
#4
诸葛欧阳2015-03-14 21:01
程序代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
  int i,j;
  string str[6]={"we","go","to","school","today"};//
  string t;
  for(i=0;i<5;i++)
    for(j=0;j<5-i;j++)
    {
     if(str[j]>str[j+1])
     {
      t=str[j];
      str[j]=str[j+1];
      str[j+1]=t;
     }
    }

  for(i=0;i<6;i++)
    cout<<str[i]<<" ";
  cout<<endl;
//cout<<str[0];说明str[0]是空格
  return 0;
}
#5
诸葛欧阳2015-03-14 21:06
程序代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
  int i,j;
  string str[6]={"we","go","to","school","today"};//
  string t;
  for(i=0;i<5;i++)
    for(j=0;j<4-i;j++)//这里不能到4,否则
    {
     if(str[j]>str[j+1])//j+1为5,正好是空格
     {
      t=str[j];
      str[j]=str[j+1];
      str[j+1]=t;
     }
    }

  for(i=0;i<5;i++)
    cout<<str[i]<<" ";
  cout<<endl;
//cout<<str[0];说明str[0]是空格
  return 0;
}

修改之后代码
#6
zcdjt2015-03-14 22:04
数组是从0开始的。
#7
蜗牛cr2015-03-15 09:08
回复 3楼 zklhp
好复杂的感觉
#8
蜗牛cr2015-03-15 09:11
回复 5楼 诸葛欧阳
不是只有5个字符串吗,为什么要定义str[6],还有str[0]为什么是空格?
#9
蜗牛cr2015-03-15 09:17
#include<iostream>
#include<string>
using namespace std;
int main()
{
  int i,j;
  string str[5]={"we","go","to","school","today"};//×¢
  string t;
  for(i=0;i<4;i++)
    for(j=0;j<4-i;j++)
    {
     if(str[j]>str[j+1])
     {
      t=str[j];
      str[j]=str[j+1];
      str[j+1]=t;
     }
    }

  for(i=0;i<5;i++)
    cout<<str[i]<<" ";
  cout<<endl;
  return 0;
}
#10
caiyumeng2015-03-18 22:29
回复 3楼 zklhp
版主 字符串还能这样比较大小啊 长见识了
#11
zklhp2015-03-18 22:50
以下是引用caiyumeng在2015-3-18 22:29:33的发言:

版主 字符串还能这样比较大小啊 长见识了

要不怎么说C++非常强大
1