forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex9_41_42.cpp
43 lines (35 loc) · 943 Bytes
/
ex9_41_42.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! @Alan
//!
//! Exercise 9.41:
//! Write a program that initializes a string from a vector<char>.
//!
//! Exercise 9.42:
//! Given that you want to read a character at a time into a string,
//! and you know that you need to read at least 100 characters,
//! how might you improve the performance of your program?
// Use member reserve(120) to allocate enough space for this string.
//!
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
//! Exercise 9.41
void str_from_vectorChar(const std::vector<char>&, std::string&);
int main()
{
//! test for str_from_vectorChar():
std::string s;
std::vector<char> v = {'a','a','a','a','c'};
str_from_vectorChar(v,s);
std::cout<< s;
return 0;
}
void str_from_vectorChar(const std::vector<char> &v, std::string &s)
{
for(auto it = v.begin(); it != v.end(); ++it)
{
s.push_back(*it);
}
}