Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters

Reply
 
Thread Tools

Old 10.04.2004, 08:12   #16
Грустно...
 
Agregat's Avatar
 
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 44
Posts: 21,717
Rep Power: 9
Default

Для любителей сцей еще вариант:
PHP Code:
std::ifstream in(path);
in.seekg(0ios_base::end);
std::basic_ios::pos_type fileSize in.tellg();
in.seekg(0ios_base::beg);
std::vector<charv(fileSize);
in.read(&v[0], fileSize)
std::string s(v.begin(), v.end()); 
туповатое решение, но не хуже остальных

Old 10.04.2004, 08:27   #17
The Reloaded
 
Aram Hambardzumyan's Avatar
 
Join Date: 01 2002
Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,387
Rep Power: 5
Default

Quote:
Originally Posted by Agregat
Тому, кто скажет почему в моих примерах перед std::copy, std::getline не нужeн std:: префикс от меня 5 очков
вычисление пространства имен по аргументам (это кажется koening lookup называется?)

Old 10.04.2004, 11:16   #18
Грустно...
 
Agregat's Avatar
 
Join Date: 08 2002
Location: Там, где всегда идут дожди
Age: 44
Posts: 21,717
Rep Power: 9
Default

Абсолютно верно. Поиск Кенига. Что функции ищутся так же в namespace-ах аргументов

Old 21.05.2020, 08:30   #19
Ego coder
 
AvDav's Avatar
 
Join Date: 07 2004
Location: Yerevan, Armenia
Age: 45
Posts: 3,738
Rep Power: 5
Default

PHP Code:
#include <iostream>
#include <string>
#include <fstream>
#include <streambuf>
#include <algorithm>

std::string readFromFile(const char *path __FILE__)
{
    
std::ifstream t(path);
    
std::string str((std::istreambuf_iterator<char>(t)), \
                     
std::istreambuf_iterator<char>());
    
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
    return 
str;
}
int main()
{
   
std::cout << "Hello World!\n" << readFromFile();


Old 25.12.2020, 01:48   #20
Ego coder
 
AvDav's Avatar
 
Join Date: 07 2004
Location: Yerevan, Armenia
Age: 45
Posts: 3,738
Rep Power: 5
Default

Вернее так, чтоб строго по постановке задачи.
PHP Code:
#include <iostream>
#include <string>
#include <fstream>
#include <streambuf>
#include <algorithm>

std::string readFromFile(const char *path __FILE__)
{
    
std::ifstream t(path);
    
std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
    
std::string::iterator oldEnd str.end();
    
str.erase(std::remove_if(str.begin(), str.end(), [](unsigned char x){return std::isspace(x);}), oldEnd);
    return 
str;
}
int main()
{
   
std::cout << "Hello World!\n" << readFromFile();
   
system("pause");
   return 
EXIT_SUCCESS;


Old 26.07.2021, 07:07   #21
Ego coder
 
AvDav's Avatar
 
Join Date: 07 2004
Location: Yerevan, Armenia
Age: 45
Posts: 3,738
Rep Power: 5
Default

Интересующемся советуется следующая литература для саморазвития:
1. "Effective STL" by Scott Meyers.
2. "C++ Standard Library, The: A Tutorial and Reference" by Nicolai M. Josuttis.
Reply




Реклама:
реклама
Buy text link .

All times are GMT. The time now is 02:21.
Top

Powered by vBulletin® Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.