#include <iostream>
#include <vector>
#include <locale>
struct whitespace : std::ctype<char>
{
static const mask* make_table()
{
static std::vector<mask> v(classic_table(), classic_table() + table_size);
v['\n'] |= space;
v[' '] &= ~space;
return &v[0];
}
whitespace(std::size_t refs = 0) : ctype(make_table(), false, refs) {}
};
int main()
{
std::string s;
std::cin.imbue(std::locale(std::cin.getloc(), new whitespace));
while(std::cin >> s)
{
std::cout << s << '\n';
}
}
и ваш вариант ну не оч
как работает scanf в плюсах?
На чем лучше написать программу для windows?
нужно именно для винды.
#include <iostream>
using ptr = void (*)();
class A
{
public:
A( ptr f = [](){} ) : _ptr{ f }{}
ptr& operator()()
{
return _ptr;
}
//...
private:
ptr _ptr;
};
int main()
{
ptr foo = [](){ std::cout << "A::_ptr" << std::endl;};
std::cout << "foo\n";
A a(foo);
std::cout << "A\n";
ptr bar = a();
std::cout << "bar\n";
bar();
}
cin.getline
istream& istream::getline( char* buffer, streamsize num );
istream& istream::getline( char* buffer, streamsize num, char delim );
std::string s;
while(std::getline(std::cin, s))
{
std::cout << s << std::endl;
}
как работает scanf в плюсах?
std::string s;
while(std::cin >> s)
{
std::cout << s << '\n';
}
но... проблема осталась.
#include <iostream>
#include <string>
std::istream& operator>>(std::istream& is, std::string& s)
{
return std::getline(is, s);
}
int main()
{
std::string s;
while(std::cin >> s)
{
std::cout << s << '\n';
}
}
template <typename T>
T& Array<T>::operator[](int i)
{
return arr[i];
}
template <typename T>
T& Array<T>::at(int i)
{
if(i < 0 || i > size)
{
throw "out of range"; // просто для примера
}
return arr[i];
}
int N;
cin >> N;
Array<int> A(N);
int value = 0;
for(int i = 0; i < N; ++i)
{
cin >> value;
A[i] = value;
}
template<typename T>
istream& operator>>(istream& is, Array<T>& container)
{
T value;
while(is >> value)
{
container.push_back(value);
//или реализовать emplace_back(T&& value)
}
return is;
}
А если осуществлять ввод строки с клавиатуры?
#include <stdio.h>
unsigned stdin_count(char symbol)
{
char c;
unsigned count = 0;
while((c = getchar()) != 10)
{
if(c == symbol) count++;
}
return count;
}
int main()
{
printf("%i", stdin_count(' '));
return 0;
}
public Button createButton()
{
return new MacOSButton();
}