Which of the following functions will take a vector of strings and return a list of integers containing the sizes of the strings without using any loops or recursion?
For example, when provided a vector v0 with the strings ["abc", "", "defg", "h"], the function f(v0) should return a list of integers [3,0,4,1].
| A. |
std::list<size_t> f(const std::vector<std::string>& v0)
{
std::list<size_t> result;
for (std::vector<std::string>::iterator it = v0.begin(), it != v0.end(), it++)
{
result.push_back((*it).size());
}
return result;
} |
|
| B. |
std::list<size_t> f(const std::vector<std::string>& v0)
{
std::list<size_t> result;
std::transform(v0.begin(), v0.end(), std::back_inserter(result), [&](const std::string& s) {
result s.size();
});
return result;
} |
|
| C. |
int* f(std::string[] v0)
{
int* result = new int[v0.length];
for (int i = 0; i < v0.length; i++)
{
result[i] = v0[i].size();
}
return result;
} |
|
| D. |
std::list<size_t> f(const std::vector<std::string>& v0)
{
std::list<size_t> result;
std::for_each(v0.begin(), v0.end(), [](const std::string& s) {
return s.size();
});
return result;
} |
Options A,C,D are using loops. So, answer is Option B
std::list<size_t> f(const std::vector<std::string>& v0)
{
std::list<size_t> result;
std::transform(v0.begin(), v0.end(), std::back_inserter(result), [&](const std::string& s) {
result s.size();
});
return result;
}
Option B
Which of the following functions will take a vector of strings and return a list of...
Using ONLY the following header functions: #include <iostream> #include <string> #include <cassert> #include <vector> using namespace std; Create a C++ function that satisfies the condition using recursion, NO WHILE OR FOR LOOPS. Pre-condition: s.size() == t.size() Post-condition: Returns a vector where the first string is the first character of s followed by the first character of t, the second string is the second character of s followed by the second character of t, and so on. For example, zip("abc", "xyz")...
#include using namespace std; vector split_string(string); // Complete the findMedian function below. int findMedian(vector arr) { } int main() { ofstream fout(getenv("OUTPUT_PATH")); int n; cin >> n; cin.ignore(numeric_limits::max(), '\n'); string arr_temp_temp; getline(cin, arr_temp_temp); vector arr_temp = split_string(arr_temp_temp); vector arr(n); for (int i = 0; i < n; i++) { int arr_item = stoi(arr_temp[i]); arr[i] = arr_item; } int result = findMedian(arr); fout << result << "\n"; fout.close(); return 0; } vector split_string(string input_string) { string::iterator new_end = unique(input_string.begin(), input_string.end(), []...
How to store a sentence of strings into a vector container with push_back until I enter and type "quit"? string str: vector < int > v: if (str != "quit") {//use push_back to store str to vector} BUILD 3 LOOPS TO DISPLAY THE USER DEFINED STRING SENTENCE WITH THE CONDITIONS: to print out the strings using a for loop with begin and end & differencing iterator pointer. In another loop using the C++11 auto keyword to simplify the declaration of...
there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 { template<class item> class node { public: typedef item value_type; ...
1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...
Sometimes when I sit in restaurants waiting on food, I request the children’s menu to play the games. One of my favorites is the word puzzle in which you search for words hidden in a scramble of letters. I began to work out a solution to this game programmatically. That seemed a little simple, so I decided to have you solve the problem of a scramble of integers, finding the sub-row or sub-column which sums to another integer. You will...
Hello, The C++ question is: ------------------------------------------------------------------------------- Write a program that takes as input 2 strings from the user. Then it determines if one string is a permutation of the other string. If the program answers "yes" to the previous question, meaning the two strings are permutations of each other, determine if each string has all unique characters. --------------------------------------------------------------------------------- I have completed the first part but I am unsure on how to also determine if each string is all unique characters...
Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here. Code: #ifndef LIST_H #define LIST_H #include using namespace std; template class List { private: // The basic doubly linked list node. // Nested inside of List, can be public // because the Node is itself private struct Node { Object data; Node *prev;...
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...
Using ONLY the following header functions: #include "cmpt_error.h" #include <iostream> #include <string> #include <vector> #include <cassert> using namespace std; Create a C++ function that satisfies the condition using recursion, NO WHILE OR FOR LOOPS. Pre-condition: a.size() == b.size(), and a.size() > 0 Post-condition: Returns a vector equal to {min(a[0],b[0]), min(a[1],b[1]), min(a[2],b[2]), ..., min(a[n],b[n])}, where n == a.size(). For example, min_vec({3, 4, 1}, {2, 5, 2}) returns the new vector {2, 4, 1}. These are the function headers: vector<int> min_vec(const vector<int>&...