mmap解法參考:- #include<bits/stdc++.h>
- #include<sys/mman.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include<unistd.h>
- using namespace std;
- int main()
- {
-
- int fd = fileno(stdin);
- struct stat sb;
- fstat(fd, &sb);
- size_t length = sb.st_size;//以上是獲取檔案描述符
-
- char *buffer = (char *)mmap(nullptr, length, PROT_READ, MAP_PRIVATE, fd, 0);
-
- int c = 0;
- vector<char> ans;
- for (int i = 0; i < length; i++) {
- if (buffer[i] != ' ') {
- if(c % 2 == 1)
- ans.push_back(' ');
- ans.push_back(buffer[i]);
- c = 0;
- } else {
- c++;
- }
- }
- cout.write(ans.data(), ans.size());
- cout << '\n';
- return 0;
- }
複製代碼 |