2014-04-06 3 views
1

사용자로부터 입력을 받아서 그 입력을 버블 정렬하여 결과를 출력하려고합니다. 내 코드 :다른 값으로 공백을 정렬하는 방법

#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; 
} 

코드는 잘 실행되고 있지만, 입력으로 문자열에 공백 또는 들여 쓰기 값을 허용하지 않습니다. 이 값을 받아 들일 수있는 방법이 있습니까?

// 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]); 
} 
+0

'cin'가이 구분 기호의 집합에 따라 입력을 분할 즉, 토큰 화됩니다. 이러한 구분 기호는 기본적으로 공백입니다. – maddin45

답변

1

하는 std::getline>> 교체, 그것은 당신에 공백이 포함 된 문자열을 읽을 수 있습니다. 사용자가 빈 줄을 입력하지 않는 한 행을 읽으십시오.

#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"; 
    } 
} 
+0

감사합니다. 공백을 읽습니다.하지만 어떻게 든 첫 번째 값을 물어 보는 것을 무시하고 두 번째 값을 요청하기 위해 점프합니다. – user3467152

+0

@ user3467152 이것은 "남은 값" '\ n'이 'n'값을 입력하기 때문입니다. 수정 사항을 편집하십시오. – dasblinkenlight

1

사용 std::getlinestd::vector<std::string> :

+0

나는 당신의 피하는'break;를 좋아한다. –

0

당신은 스트림에서 모든 토큰을 읽을 std::getline를 사용할 수 있습니다

//Get array elements 
getline(cin, arr[0]); 
for(int i = 0; i < n ; ++i) 
{ 
    cout << "<" << i+1 << "> "; 
    getline(cin, arr[ i]); 
} 
+0

이제는 공백을 읽는다.하지만 어떻게 든 첫 번째 값을 물어 보는 것을 무시하고 두 번째 값을 묻기 위해 점프한다. – user3467152

+0

@ user3467152 이것은 남은 '\ n' 이전 읽기에서 – 4pie0

관련 문제