0% found this document useful (0 votes)
40 views4 pages

Erasing From Vector

The document discusses erasing elements from a vector in C++. It initializes a vector with values from 1 to 10, then erases the 6th element and the first 3 elements. It prints the resulting vector, which contains elements 4 through 10.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

Erasing From Vector

The document discusses erasing elements from a vector in C++. It initializes a vector with values from 1 to 10, then erases the 6th element and the first 3 elements. It prints the resulting vector, which contains elements 4 through 10.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

// erasing from vector

#include <iostream>
#include <vector>
using namespace std;

int main ()
{
unsigned int i;
vector<unsigned int> myvector;

// set some values (from 1 to 10)


for (i=1; i<=10; i++) myvector.push_back(i);

// erase the 6th element


myvector.erase (myvector.begin()+5);

// erase the first 3 elements:


myvector.erase (myvector.begin(),myvector.begin()+3);

cout << "myvector contains:";


for (i=0; i<myvector.size(); i++)
cout << " " << myvector[i];
cout << endl;

return 0;
}

Output:
myvector contains: 4 5 7 8 9 10
  #include <iostream> 
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <numeric> 
    using namespace std; 

    int main() 


    {
        vector<string> student_names;
        vector<double> student_marks;

        do 
        {
            string name;
            cout << "Enter student name (Enter to finish): "; 
            getline (cin, name);

                // Now that we have the name from the user, 


                // append it at the end of the vector if valid 
            if (name != "")
            {
                student_names.push_back (name);
            }
  
        }
        while (name != "");

            // sort them alphabetically


        sort (student_names.begin(), student_names.end());

            // Now enter grades for each student

        student_marks.resize (student_names.size());

        vector<string>::iterator s;
        vector<double>::iterator m;
        for (s = student_names.begin(), m = student_marks.begin();
             s != student_names.end();
             ++s, ++m) 
        { 
            do
            {
                cout << "Enter marks (0 to 100) for " << *s << ": ";
                cin >> *m;

                if (*m < 0 || *m > 100)


                {
                    cout << "Error: marks must be between 0 and 100" << endl;
                }
            }
            while (*m < 0 || *m > 100);
        }

        if (! students_marks.empty())    // equivalent to student_marks.size()


!= 0
        {
            cout << "There are " << count (student_marks.begin(),
student_marks.end(), 100) 
                 << " students that got highest mark!" << endl;  

            cout << "Lowest mark: " 


                 << *min_element (student_marks.begin(), student_marks.end()) 
                 << "\nHighest mark: " 
                 << *max_element (student_marks.begin(), student_marks.end())
<< endl;

            cout << "Average: " 


                 << accumulate (student_marks.begin(), student_marks.end(), 0)
/
                    student_marks.size() << endl;
        }

        return 0; 
    } 
#include <vector>
#include <iostream>
using namespace std;

float sum(const vector<float>& x); // prototype

//====================================================== main
int main() {
vector<float> a; // Declare a vector.

float temp;
while (cin >> temp) {
a.push_back(temp);
}

cout << "Average = " << sum(a)/a.size() << endl;

return 0;
}

//======================================================= sum
// sum adds the values of the vector it is passed.
float sum(const vector<float>& x) {
float total = 0.0; // the sum is accumulated here
for (int i=0; i<x.size(); i++) {
total = total + x[i];
}
return total;
}

You might also like