C++ 如何使用其他值对空格进行冒泡排序?

C++ 如何使用其他值对空格进行冒泡排序?,c++,C++,我试图从用户那里获取一个输入,并对该输入进行冒泡排序,然后输出结果。我的代码: #include<iostream> using namespace std; class bubble { public : string arr[20]; //Number of elements in array int n; //Function to accept array eleme

我试图从用户那里获取一个输入,并对该输入进行冒泡排序,然后输出结果。我的代码:

    #include<iostream>
    using namespace std;
    class bubble
    {
    public :

        string arr[20];

        //Number of elements in array
        int n;

        //Function to accept array elements
        void read()
        {
            while(1)
            {
                cout<<"\nEnter the number of elements in the array:";
                cin>>n;
                if(n<=20)

                    break;
                else
                    cout<<"\n Array can have maximum 20 elements \n";
            }
            //display the header
            cout<<"\n";
            cout<<"----------------------\n";
            cout<<"Enter array elements \n";
            cout<<"----------------------\n";

            //Get array elements
            for( int i=0; i<n ;i++ )
            {
                cout<<"<"<<i+1<<"> ";
                cin>>arr[i];
            }
        }
        //Bubble sort function
        void bubblesort()
        {
            for( int i=1;i<n ;i++ )//for n-1 passes
            {
                //In pass i,compare the first n-i elements
                //with their next elements
                for( int j=0; j<n-1; j++)
                {
                    if(arr[j] > arr[j+1])
                    {
                        string temp;
                        temp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = temp;

                    }

                }
            }
        }
        void display()
        {
            cout<<endl;
            cout<<"----------------------\n";
            cout<<"Sorted array elements \n";
            cout<<"----------------------\n";
            for( int j=0; j<n; j++)
                cout<<arr[j]<<endl;
        }
};
int main()
{
    //Instantiate an instance of class
    bubble list;
    // Function call to accept array elements
    list.read();
    // Function call to sort array
    list.bubblesort();
    //Function call to display the sorted array
    list.display();
    return 0;
}
#包括
使用名称空间std;
阶级泡沫
{
公众:
字符串arr[20];
//数组中的元素数
int n;
//函数接受数组元素
无效读取()
{
而(1)
{
coutn;

如果(n将
>
替换为,则可以读取包含空格的字符串:

// Extract the \n that's left from entering n
getline(cin, arr[0]); // We'll read it again in the loop
for( int i=0; i<n ;i++ )
{
    cout<<"<"<<i+1<<"> ";
    getline(cin, arr[i]);
}
//从输入n中提取剩余的\n
getline(cin,arr[0]);//我们将在循环中再次读取它

对于(int i=0;i使用
std::getline
std::vector
。只要用户没有输入空行,就可以读取行

#include <iostream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> strings;

    std::string line;
    bool user_wants_to_quit = false;
    while (!user_wants_to_quit && std::getline(std::cin, line))
    {
        if (!line.empty())
        {
            strings.push_back(line);
        }
        else
        {
            user_wants_to_quit = true;
        }
    }

    std::cout << "Lines:\n";
    for (std::vector<std::string>::const_iterator iter = strings.begin(); iter != strings.end(); ++iter)
    {
        std::cout << *iter << "\n";
    }
}
#包括
#包括
#包括
int main()
{
std::向量字符串;
std::字符串行;
bool用户想要退出=false;
而(!user_希望_退出&&std::getline(std::cin,line))
{
如果(!line.empty())
{
字符串。推回(行);
}
其他的
{
用户希望退出=真;
}
}
std::cout您可以使用从流中读取所有令牌:

//Get array elements
getline(cin, arr[0]);
for( int i = 0; i < n ; ++i)
{
    cout << "<" << i+1 << "> ";
    getline( cin, arr[ i]);
}
//获取数组元素
getline(cin,arr[0]);
对于(int i=0;icout
cin
被标记化,这意味着它根据一组分隔符分割输入。默认情况下,这些分隔符是空白。感谢您,现在它读取空格,但不知何故它忽略了请求第一个值,并跳到请求第二个值value@user3467152这是因为“剩菜”
“\n”
从输入
n
的值开始。请查看编辑以获取修复。现在它读取空格,但不知何故它忽略了请求第一个值,并跳到请求第二个值value@user3467152这是因为首先我们需要从前面的read中读取剩余的'\n',就像您避免
break;