Need help on modifying an array to vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nicolecoop
    New Member
    • Mar 2013
    • 2

    Need help on modifying an array to vectors

    Hi how are you guys? I just wanted to ask a favor if you could find the syntax error and tell me what I am doing wrong. My prof. just taught the class about Vectors and she wanted us to modify an array program into a vector.

    The array program I made includes a sort function that sorts an array of strings in alphabetical order.


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    void sort(string cookie[], int n);
    void print(string cookie[], int n);
    
    
    int main()
    {
    int rows=0;
    char filename[30];
    string names[100];
    
    
    ifstream alphabet;
    alphabet.open ("letters.txt");
    
    if (alphabet.fail())
    {
    cerr << "Error opening input file." << endl;
    exit(1);
    }
    
    while (getline(alphabet,names[rows])) 
    {
    rows++;
    }
    
    sort(names,rows);
    print(names,rows);
    
    
    alphabet.close();
    return 0;
    }
    
    void sort(string cookie[], int n) 
    {
    int i, j;
    string temp;
    
    for(i=1; i<n; i++) 
    {
    temp = cookie[i];
    for(j=i; j>=1 && (temp < cookie[j-1]); j--) 
    {
    cookie[j] = cookie[j-1];
    cookie[j-1] = temp;
    }
    }
    }
    
    void print(string cookie[], int n) 
    {
    for(int i=0; i<n; i++) 
    {
    cout << cookie[i] << endl;
    }
    }
    And this is my version of trying to do it in a vector:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    void sort(string cookie, int n);
    void print(string cookie, int n);
    vector<char>filename;
    vector<char>names;
    
    int main()
    {
    int rows=0;
    string cookie;
    
    
    ifstream alphabet;
    alphabet.open ("letters.txt");
    
    if (alphabet.fail())
    {
    cerr << "Error opening input file." << endl;
    exit(1);
    }
    
    while (getline(alphabet,names[rows])) 
    {
    rows++;
    }
    
    sort(cookie,rows);
    print(cookie,rows);
    
    
    alphabet.close();
    return 0;
    }
    
    void sort(string cookie, int n) 
    {
    unsigned int i, j;
    unsigned char temp;
    unsigned int cookiesize = cookie.size()-1;
    
    for(i = 0; i <= cookiesize; i++)
    {
    names.push_back(cookie[i]);
    }
    
    for(i=1; i<names.size()-1; i++) 
    {
    unsigned int totalsize = names.size()-1;
    
    temp = cookie[i];
    for(j=i; (j>=1 && temp < cookie[j-1]); j--) 
    {
    cookie[j] = cookie[j-1];
    cookie[j-1] = temp;
    }
    }
    }
    
    void print(string cookie[], int n) 
    {
    for(int i=0; i<n; i++) 
    {
    cout << cookie[i] << endl;
    }
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    A vector is an array. In your case a vector<string>.

    Use vector::push_ba ck() to add your strings to the vector.

    Use the std::sort() to sort your strings by passing in iterators set to vector::begin() and vector::end().

    To print, run your loop from vector::begin() to
    !vector::end(). cout << will work with string::c_str() .

    That's about it.

    Comment

    • divideby0
      New Member
      • May 2012
      • 131

      #3
      The functions should also be changed to take the vector as an argument and not the string.

      Code:
      void mysort(vector<string> &); 
      void myprint(const vector<string> &);
      Perhaps wfc can clarify, but I *think* you'll need to change the file read too. I don't believe you can use an empty vector the same way as you're doing with the string array in getline.

      Code:
      vector<string> names;
      string fbuf;
      
      while(getline(alphabet, fbuf))
          names.push_back(fbuf);

      Comment

      • nicolecoop
        New Member
        • Mar 2013
        • 2

        #4
        I am not that good with C++ and my professor just taught us vectors like two days ago so I am still confuse about the syntax that goes with it ..

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Use the Internet. Seach on vector+ C++ and you will find example code.

          This web site will help you with your code but teaching and code writing are outside the scope ofthe site.

          Comment

          Working...