Question

In: Computer Science

C++ Sort Vector of Strings (case-sensitive) Without using libraries, how would I arrange a vector like...

C++ Sort Vector of Strings (case-sensitive)

Without using libraries, how would I arrange a vector like this alphabetically?

vector words {"September", "test" "seven", "apple"}

The output should be: "apple, seven, September, test"

Solutions

Expert Solution

#include <iostream>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;

int main()
{
    vector<string> array;
    string temp;
    int i,j;
    array.push_back("september");
    array.push_back("test");
    array.push_back("seven");
    array.push_back("apple");
  
  
    for(i=0;i<array.size();i++)
    {
        //temp=array[i];
        for(j=i+1;j<array.size();j++)
        {
            if(array[i]>array[j])
            {
                temp=array[i];
                array[i]=array[j];
                array[j]=temp;
            }
        }
    }
    cout<<array[0]<<endl;
    cout<<array[1]<<endl;
    cout<<array[2]<<endl;
    cout<<array[3]<<endl;
    return 0;
}

code:

output:

If you have any queries...please comment...Thank you...


Related Solutions

i have an array of strings, how do i sort them into a binary tree? C...
i have an array of strings, how do i sort them into a binary tree? C Program
I would like to integrate a bubble sort into this binary search in c ++ Thank...
I would like to integrate a bubble sort into this binary search in c ++ Thank you! #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) {         while (f <= l)         {             int m = f + (l - l) / 2;                 // Check if...
Change the code to sort list of strings without using any pre-defined functions Here is my...
Change the code to sort list of strings without using any pre-defined functions Here is my code: int n; String temp; Scanner in = new Scanner(System.in); System.out.print("Enter number of names you want to enter:"); n = in.nextInt(); String names[] = new String[n]; Scanner s1 = new Scanner(System.in); System.out.println("Enter all the names:"); for(int i = 0; i < n; i++){ names[i] = s1.nextLine(); } ***CHANGE THIS PART*** for (int i = 0; i < n; i++){ for (int j = i...
Using only core C++ (no special libraries, except STL vector or string if you want), write...
Using only core C++ (no special libraries, except STL vector or string if you want), write a C++ program that allows a user to input a string and (a) Checks if the expression is a valid polynomial. Parentheses or negation are not allowed. Spaces should be ignored. E.g., the following are valid i. n^2+2*n+5 ii. 2*n + 4.54* n^5 +4 +5*n and the following are invalid iii. n^3n iv. n^4.2 v. 5n vi. n^3 -3*n if an input is given...
C++ How would I sort my output to evaluate it by sorting the column by it's...
C++ How would I sort my output to evaluate it by sorting the column by it's size? I didn't include my full program but here is the main.cpp where i'm supposed to sort the output by column size. //User Libraries #include <cstdlib> #include <ctime> #include <iostream> using namespace std; //User Libraries #include "Table.h" #include "Triangle.h" //Global Constants //Function Prototype void prntRow(RowAray *,int); void prntTab(Table *); void prntTri(Triangle *); //Execution Begins Here! int main(int argc, char** argv) { //Initialize the random...
How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
I would like the answers for these case study questions showing clearly how they are calculated.  ...
I would like the answers for these case study questions showing clearly how they are calculated.   Issuer Face Value Coupon Rate     Rating Quoted Price $ Years Until Maturity Sinking Fund Call Period ABC Energy 1000 6% AAA 809.10 20 Yes 3 Yrs ABC Energy 1000 0% AAA 211.64 20 Yes N/A Trans Power 1000 10% AA 1025.00 20 Yes 5 Yrs Telco Utilities 1000 12% AA 1300.00 30 No 5 Yrs 1. One of Jill's best clients poses the...
In c++ how do I search a vector for a name and then determine if the...
In c++ how do I search a vector for a name and then determine if the name is found?
In the case of c++, how would I store data drom a recursive function into a...
In the case of c++, how would I store data drom a recursive function into a 2D array?
I need to write a C++ program that appends "not" into the string without using the...
I need to write a C++ program that appends "not" into the string without using the append method or any standard libraries. It should return the string if there isn't an "is" in it. Examples: is is = is not is not This is me = This is not me What is yellow? = What is not yellow? The sky is pink = The sky is not pink isis = isis What happened to you? = What happened to you?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT