Question

Given: set<string> s; Write C++ code segment to a) Add the string Sam, Sally, Sue b)...

Given: set<string> s; Write C++ code segment to

a) Add the string Sam, Sally, Sue

b) Print all the strings in the set, using an iterator

Set has the following methods: begin(), end(), insert(item)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <set>

using namespace std;

int main(){
    set<string> s;
    set<string>::iterator it;

    s.insert("Sam");
    s.insert("Sally");
    s.insert("Sue");
    
    for (it=s.begin(); it!=s.end(); ++it){
        cout << *it << endl;
    }
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Given: set<string> s; Write C++ code segment to a) Add the string Sam, Sally, Sue b)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /**...

    Create a linked list with given code Given code: import java.util.Iterator; public interface Set { /** Removes all of the elements from this set */ void clear(); /** Returns true if this set contains no elements @return true if this set contains no elements */ boolean isEmpty(); /** Returns the number of elements in this set (its cardinality) @return the number of elements in this set */ int size(); /** Returns an iterator over the elements in this set @return...

  • Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using...

    Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using namespace std; class List; class Iterator; class Node { public: /* Constructs a node with a given data value. @param s the data to store in this node */ Node(string s); /* Destructor */ ~Node() {} private: string data; Node* previous; Node* next; friend class List; friend class Iterator; }; class List { public: /** Constructs an empty list. */ List(); /* Destructor. Deletes...

  • Plz code in c++ below ----------------------------------- Given a string variable word that has been declared and...

    Plz code in c++ below ----------------------------------- Given a string variable word that has been declared and given a value, write a short code block that tests whether the variable holds the string "programmer". If it does, print the message "Get coding...", otherwise print "Before" or "After" depending on whether the word comes before or after programmer in the dictionary. In all cases, end with a newline.

  • 40pts) Given the following code segment. mSwer= input("Enter age: ") ile answer != 'q' : try:...

    40pts) Given the following code segment. mSwer= input("Enter age: ") ile answer != 'q' : try: print(int(answer) ) answer input ( " Enter age: ") except Value E rror : print ("Invalid answer 'q' age") Show what is printed if the user enters the following values at the prompt, one at a time. Print output User input 20 17 2a => 29 q 9. The following main function works with a class called Student that you will write. def main()...

  • 2. (30 pts) Given the single-linked list below, assume that the variable tail references the last...

    2. (30 pts) Given the single-linked list below, assume that the variable tail references the last Node SingleLinkedList Node Node Node Node head next data = next data = next = data next null data = String Strin String String value "Tom" value "Dick" value"Harry" value "Sam" Write the code to do each of the following operations. Assume that the result of each operation does not affect the other operations. In another word, assume that each operation always starts with...

  • Consider the following code that repeats a C++ string three times. string a = "Hello"; string...

    Consider the following code that repeats a C++ string three times. string a = "Hello"; string b = a + a + a; Suppose s is a C string, and t is declared as char t[100]; Write the equivalent code for C strings that stores the threefold repetition of s (or as much of it as will fit) into t. (C++)

  • Write a routine which will take a string of characters consisting of just the letters A B and C and rearrange the string so that at the end of the code

    In JavaWrite a routine which will take a string of characters consisting of just the letters A B and C and rearrange the string so that at the end of the code there will first be all the "A" 's then all the "B" 's and then all the "C" 's. Example string "ACBBACABC" should end up "AAABBBCCC"

  • 1. Consider the following code segment. String str = "AP"; str += "CS " + 1...

    1. Consider the following code segment. String str = "AP"; str += "CS " + 1 + 2; System.out.println(str); What is printed as a result of executing the code segment? A: CS AP12 B: AP CS3 C: CSAP 12 D: APCS 12 E: APCS 3 2. Consider the following code segment. String dessert = "pie"; dessert += "straw" + dessert + "berry"; What is the value of dessert after the code segment has been executed? A: strawpieberry B: piestrawpieberry C:...

  • IN C++ PLEASE Write a class Food that has a string attribute for name and a...

    IN C++ PLEASE Write a class Food that has a string attribute for name and a Boolean value for hasBeenEaten and a Boolean value for isSpoiled, default both Booleans to false add a method spoil() which sets isSpoiled to true Add a method eat() If hasBeenEaten is false, if isSpoiled is false, return a string that says "you eat the <insert name here>, yum!", then set hasBeenEaten to true if isSpoiled is true, return a string that says "I wouldn't...

  • 5. (A) Write an inductive definition for the following set: (please include ALL steps) S: lam...

    5. (A) Write an inductive definition for the following set: (please include ALL steps) S: lam b^2m | where me N and m >0} *Sis a set of strings, aam bn means amb (B) Write pseudo-code of a recursive function f(x, y) to check whether string x and string y are equal. The alphabet is (a.b). Hint for a given string P, you can verify if it is aQ or bQ (where Q is the remainder of string P) (10...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT