簡體   English   中英

動態分配字符串數組

[英]Dynamically Allocating String Arrays

到目前為止,這就是我所擁有的。 我試圖在C ++中編輯動態分配的數組,但是,當for循環運行時,它跳過了第一項。 我需要它來獲取所有物品。 任何幫助表示贊賞。 提前致謝。

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


int main()
{
    // Declare Variables
    int userChoice = 0;
    int numItems = 0;

    cout << "How many items will be on your list? ";
    cin >> numItems;

    string *list = new string[numItems];

    // Give the user some options
    cout << "1. Add Item" << endl;
    cout << "2. Remove Item" << endl;
    cout << "3. Sort Items" << endl;
    cout << "4. Exit" << endl;
    cout << "Enter the number of the operation you wish to perform: ";
    cin >> userChoice;
    cout << endl;

    // Perform the operation
    switch(userChoice)
    {
    case 1:
        {
            cin.clear();    // Remove new line from cin

            for(int i = 0; i < numItems; i++)
            {
                cout << "Item #" << i + 1 << " --> ";
                getline(cin, list[i]);
            }
        }
        break;
    case 2:
        {

        }
        break;
    case 3:
        {

        }
        break;
    case 4:
        {
            return 0;
        }
    default:
        {
            cout << "Error! Invalid Selection" << endl;
        }

    }

    // Output the list
    cout << "-------Items-------" << endl
         << *list << endl << endl;

    // free memory
    delete [] list;

    cout << "Enter the number of the operation you wish to perform: ";
    cin >> userChoice;


    return 0;
}

對於某些事物(例如字符串)使用STL,然后使用標准數組保存字符串,這對我來說似乎很奇怪。 同樣, list是STL中對象的標准類型,因此對於變量而言,這不是一個好名字。 修改后的代碼解決了忽略第一行的問題,並且使用了vector而不是執行newdelete

#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <vector>
#include <iterator>
using namespace std;

int main()
  {
  // Declare Variables
  int userChoice = 0;
  int numItems = 0;

  cout << "How many items will be on your myList? ";
  cin >> numItems;
  cin.ignore ();

  vector<string> myList;

  while (true)
    {
    // Give the user some options
    cout << "1. Add Item" << endl;
    cout << "2. Remove Item" << endl;
    cout << "3. Sort Items" << endl;
    cout << "4. Exit" << endl;
    cout << "Enter the number of the operation you wish to perform: ";
    cin >> userChoice;
    cin.ignore ();
    cout << endl;

    // Perform the operation
    switch(userChoice)
    {
    case 1:
        {
        for(int i = 0; i < numItems; i++)
            {
            cout << "Item #" << i + 1 << " --> ";
            string s;
            getline(cin, s);
            myList.push_back (s);
            }
        }
        break;
    case 2:
        {

        }
        break;
    case 3:
        {
        sort (myList.begin (), myList.end ());
        }
        break;
    case 4:
        {
            return 0;
        }
    default:
        {
            cout << "Error! Invalid Selection" << endl;
        }

    }

    // Output the myList
    cout << "-------Items-------" << endl;
    copy(myList.begin(), myList.end(), ostream_iterator<string>(cout, "\n"));

  } // end of while
}

項目說明明確指出我不能使用標准容器,排序函數或智能指針

請在下面重做以免使用那些東西。 :)

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

int myCompare (const void * a, const void * b)
  {

  if ((*(string *) a) < *(string *) b) return -1;
  if ((*(string *) a) > *(string *) b) return 1;
  return 0;
  }

int main()
  {
  // Declare Variables
  int userChoice = 0;
  int numItems = 0;

  cout << "How many items will be on your myList? ";
  cin >> numItems;
  cin.ignore ();

  string *myList = new string[numItems];

  while (true)
    {
    // Give the user some options
    cout << "1. Add Item" << endl;
    cout << "2. Remove Item" << endl;
    cout << "3. Sort Items" << endl;
    cout << "4. Exit" << endl;
    cout << "Enter the number of the operation you wish to perform: ";
    cin >> userChoice;
    cin.ignore ();
    cout << endl;

    // Perform the operation
    switch(userChoice)
    {
    case 1:
        {
        for(int i = 0; i < numItems; i++)
            {
            cout << "Item #" << i + 1 << " --> ";
            getline(cin, myList [i]);
            }
        }
        break;
    case 2:
        {

        }
        break;
    case 3:
        {
        qsort (myList, numItems, sizeof (string *), myCompare);
        }
        break;
    case 4:
        {
            delete [] myList;
            return 0;
        }
    default:
        {
            cout << "Error! Invalid Selection" << endl;
        }

    }

    // Output the myList
    cout << "-------Items-------" << endl;
    for(int i = 0; i < numItems; i++)
      cout << myList [i] << endl;

  } // end of while
}

for循環不會跳過第一個元素。 第一個元素只是一個空行。

因為以下內容清除了錯誤標志。

cin.clear();    // Remove new line from cin --> No!!!!

如果要跳過直到新行,則必須改用ignore()

cin.ignore(numeric_limits<streamsize>::max(), '\n'); // this removes until newline from cin !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM