forked from douglascraigschmidt/CPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
81 lines (60 loc) · 1.78 KB
/
Copy pathmain.cpp
File metadata and controls
81 lines (60 loc) · 1.78 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Uses a Array to reverse a name and test various properties of class
// Array<>.
#include <assert.h>
#include <iterator>
#include <functional>
#include <iostream>
#include <cstdio>
#include <string>
// #define RVALUE_REFERENCES
#include "Array.h"
static const char PROMPT[] = "please enter your name..: ";
static const int PROMPT_LENGTH = sizeof (PROMPT);
static const Array<char>::value_type DEFAULT_VALUE = 'D';
/**
* Prompt the user to enter their name. Note value semantics for both
* param and return value.
*/
static Array<char>
get_name(Array<char> prompt) {
// Prompt the user.
std::copy (prompt.begin(),
prompt.end(),
std::ostream_iterator<char> (std::cout));
std::string name;
std::getline (std::cin, name);
Array<char> a;
std::copy (name.begin(),
name.end(),
std::back_inserter (a));
return a;
}
#if defined(RVALUE_REFERENCES)
#define moveit std::move
#else
#define moveit
#endif /* RVALUE_REFERENCES */
int
main (int argc, char *argv[])
{
Array<char> prompt (PROMPT_LENGTH);
std::copy(PROMPT,
PROMPT + PROMPT_LENGTH,
prompt.begin());
// Pass param by value and return result by value (may or may not be
// optimized).
Array<char> a = get_name(moveit(prompt));
std::cout << "your name backwards is..: ";
std::reverse_copy(a.begin(),
a.end(),
std::ostream_iterator<char> (std::cout));
std::cout << std::endl;
// Perform an assignment (may or may not be optimized).
prompt = moveit(a);
std::cout << "your name forwards is..: ";
std::copy(prompt.begin(),
prompt.end(),
std::ostream_iterator<char> (std::cout));
std::cout << std::endl;
return 0;
}