2019/c++
string manipulations ( pointers )
fw93
2018. 3. 22. 20:05
string append 할 일이 생기는 경우:
vector<char> 을 사용해 reserve 후 append 해서 string sam(t.begin(),t.end()) 해도 되고
후에 복사하는 시간 아끼려면
string sam = ""
sam.reserve(100)
// 될꺼 같았는데 안돔 ㅜㅜ
// 걍 벡터에 옮겨서 작업해야될지도?
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | #include "stdafx.h" #include <cstdio> //#include <string> #include <iostream> #include <vector> using namespace std; void spaceconverter(string & data) { char * tmp = &data[0]; while (*tmp) { if (*tmp == ' ') { char * end = tmp; while (*end) { end++; } while (tmp<end) { *(end + 2) = *end; end--; } *tmp = '%'; *(tmp + 1) = '2'; *(tmp + 2) = '0'; } tmp++; } } string compress2(string & data) { vector<char> result; int count = 1; char target = data[0]; for (int i = 1; i < data.size(); i++) { if (data[i] == target) { count++; } else { result.push_back(target); result.push_back(count + '0'); target = data[i]; count = 1; } } result.push_back(target); result.push_back(count + '0'); string ret(result.begin(), result.end()); return ret; } void undo(char * data) { char * tmp = data; while (*tmp) { char target = *tmp; tmp++; int count = *tmp - '0'; tmp++; char * end; if (count == 1) { end = tmp; while (*end) { *(end - 1) = *end; end++; } *(end - 1) = '\0'; tmp--; } else if (count == 2) { *(tmp - 1) = target; } else { end = tmp; while (*end) { end++; } while (tmp<=end) { *(end + (count - 2)) = *end; end--; } for (int i = 0; i < count - 1; i++) { *(tmp - 1 + i) = target; } tmp = tmp + count - 2; } } return; } void compress(char * data) { int o = 0; while (data[o] != '\0') { o++; } char * tmp = data; int count = 1; int target = data[0]; tmp++; int flag = 0; while (*tmp || flag == 0) { if (!*tmp) { flag++; } if (*tmp == target) { count++; tmp++; } else { if (count == 1) { char * end = tmp; while (*end) { end++; } while (tmp <= end) { *(end + 1) = *end; end--; } *(tmp) = '0' + 1; tmp = tmp + 1; } else if (count == 2) { *(tmp - 1) = '0' + 2; } else { int d = count - 2; char * end = tmp; while (*end) { *(end - d) = *end; end++; } *(end - d) = '\0'; tmp = tmp - d; *(tmp - 1) = count + '0'; } target = *tmp; count = 1; if (flag == 0) { tmp++; } } printf("%c %d\n", target, count); } int c = 0; while (data[c] != '\0') { c++; } if (o > c) { return; } else { undo(data); return; } } void testfunc() { string s = "sam hamington"; cout << s.size() << endl; s[3] = '\0'; cout << s.size() << endl; } int main() { string fuck = "hello world"; fuck.insert(fuck.begin() + 3, 'c'); cout << fuck.at(1) << endl; while (1) {} //testfunc(); //string sam = "abcdef"; string data = "abcdefcccccddddddd"; char * sam = new char[100]; for (int i = 0; i < data.size(); i++) { sam[i] = data[i]; if (i == data.size() - 1) { sam[i + 1] = '\0'; } } compress(sam); printf("%s\n", sam); string jin = "hello world sma"; spaceconverter(jin); printf("%s\n", jin.c_str()); while (1) {} return 0; } | cs |