#include<iostream>
#include<string>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
system("chcp 1251 > null");
string s;
cout << "Введите имя: ";
while(getline(cin, s))
{
if(s.empty())
{
cout << "Вы не ввели имя!\n"
<< "Введите имя: ";
continue;
}
break;
}
cout << "\nВаше имя: " << s
<< "\nНажмите любую клавишу...";
cin.get();
}
#include<iostream>
#include<string>
#include<algorithm>
#include<cctype>
#include<clocale>
using namespace std;
/* functions from https://code-examples.net/ru/q/34ef7 */
// trim from start (in place)
static inline void ltrim(string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](const auto ch) {
return !isspace(ch);
}));
}
// trim from end (in place)
static inline void rtrim(string &s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](const auto ch) {
return !isspace(ch);
}).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(string &s)
{
ltrim(s);
rtrim(s);
}
int main()
{
setlocale(LC_ALL, "Russian");
system("chcp 1251 > null");
string s;
cout << "Введите имя: ";
while(getline(cin, s))
{
//upd
replace_if(s.begin(), s.end(), [](const auto c){return iscntrl(c); }, ' ');
trim(s);
//end upd
if(s.empty())
{
cout << "Вы не ввели имя!\n"
<< "Введите имя: ";
continue;
}
break;
}
cout << "\nВаше имя: " << s
<< "\nНажмите любую клавишу...";
cin.get();
}
#include <algorithm>
#include <iostream>
using namespace std;
template<typename T, int N, int M>
auto count_match(T(&a)[N], T(&b)[M])
{
size_t match_count = 0;
sort(begin(b), end(b));
for(auto& val : a)
{
if(binary_search(begin(b), end(b), val))
{
++match_count;
}
}
return match_count;
}
int main()
{
int v[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 22};
int v2[] = {0, 22, 33, 5, 4, 8, 9, 0, 13};
std::cout << count_match(v, v2);
cin.get();
}
Практическое руководство. Изменение фрагмента кода после его вставки в код
Используйте клавишу TAB для перемещения от одной точки замены до следующей. Для перемещения к предыдущей точке замены используйте сочетание клавиш SHIFT + TAB.
#include<iostream>
#include<string>
#include<algorithm>
//ru.cppreference.com/w/cpp/algorithm/count
using namespace std;
int main()
{
string s = "Asdk6949_glsjg+()(& *&%^%$df gdfg e$T#%Y KNUYNL GIK5654";
cout << count_if(s.begin(), s.end(), ::isalpha) << endl;
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<limits.h>
int main()
{
srand((unsigned)time(NULL));
const int n = 4;
const int m = 3;
int idx = 0;
int summa = 0;
int min_sum = INT_MAX;
int a[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a[i][j] = rand() % 20;
summa += a[i][j];
printf("a[%d][%d]=%d\n", i, j, a[i][j]);
}
if(min_sum > summa)
{
min_sum = summa;
idx = i;
}
printf("summa[%d]=%d\n", i, summa);
summa = 0;
}
printf("min summa[%d]=%d\n", idx, min_sum);
for(int i = 0; i < m; i++)
{
printf("a[%d][%d]=%d\n", idx, i, a[idx][i]);
}
return 0;
}
// Functions array
uint8_t functions_index;
int (*functions[NUMBER_FUNCTIONS])(String);
char * functions_names[NUMBER_FUNCTIONS];
void function(char * function_name, int (*f)(String)){
functions_names[functions_index] = function_name;
functions[functions_index] = f;
functions_index++;
}
#include<iostream>
#include<string>
/*
* Будем считать, что std::string это String
*/
#define NUMBER_FUNCTIONS 10
struct aRest
{
uint8_t functions_index;
int (*functions[NUMBER_FUNCTIONS])(std::string);
char* functions_names[NUMBER_FUNCTIONS];
//...
aRest() : functions_index(0){};
void function(char * function_name, int (*f)(std::string))
{
functions_names[functions_index] = function_name;
functions[functions_index] = f;
functions_index++;
}
};
class App
{
aRest bt_rest;
public:
explicit App() : bt_rest(){};
//...
int startManufacturing(std::string command);
void setup();
void call(std::string s);// Для теста. Там то же по другому...
//...
};
int App::startManufacturing(std::string command)
{
std::cout << "startManufacturing\n"
<< "with command " << command
<< " " << this << std::endl;
return 1;
}
void App::setup()
{
bt_rest.function("startManufacturing", (int(*)(std::string))&startManufacturing);
//reinterpret_cast<int(*)(std::string)>(&startManufacturing)
}
void App::call(std::string s)
{
bt_rest.functions[0](s);
}
int main()
{
App ap;
ap.setup();
ap.call("COMMAND");
}
#include<iostream>
#include<string>
#include<functional>
#define NUMBER_FUNCTIONS 10
template<typename T>
struct aRest
{
uint8_t functions_index;
int (T::*functions[NUMBER_FUNCTIONS])(std::string);
char* functions_names[NUMBER_FUNCTIONS];
//...
aRest() : functions_index(0){};
void function(char * function_name, int(T::*f)(std::string))
{
functions_names[functions_index] = function_name;
functions[functions_index] = f;
functions_index++;
}
};
class App
{
aRest<App> bt_rest;
public:
App() : bt_rest(){};
//...
int startManufacturing(std::string command);
void setup();
void call(std::string fn);
//...
};
int App::startManufacturing(std::string command)
{
std::cout << "startManufacturing\n"
<< "with command " << command
<< " " << this << " " << std::endl;
return 1;
}
void App::setup()
{
bt_rest.function("startManufacturing", &startManufacturing);
}
void App::call(std::string s)
{
std::mem_fn(bt_rest.functions[0])(this, s);
}
int main()
{
App ap;
ap.setup();
ap.call("COMMAND");
}
#include <stdio.h>
#include <wchar.h>
#include <windows.h>
int main()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE)
{
return GetLastError();
}
DWORD dwMode = 0;
if (!GetConsoleMode(hOut, &dwMode))
{
return GetLastError();
}
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hOut, dwMode))
{
return GetLastError();
}
wprintf(L"\u001B[31mRed Text!\u001B[0mNormal Text\r\n");
getchar();
return 0;
}
OUT:
#include <iostream>
#include <memory>
#include <unordered_map>
#include <list>
struct Listener
{
std::string _name;
Listener(std::string name) : _name{ name }{}
~Listener() = default;
};
using Listeners = std::unordered_map<std::string, std::list<Listener>>;
class EventManager
{
public:
EventManager(std::list<std::string>& eventTypes);
//...
~EventManager() = default;
void print()
{
for(const auto& v : *m_listeners)
{
std::cout << v.first << " -> ";
for(const auto& s : v.second)
{
std::cout << s._name << ' ';
}
std::cout << std::endl;
}
}
private:
std::unique_ptr<Listeners> m_listeners;
};
EventManager::EventManager(std::list<std::string>& eventTypes)
: m_listeners { std::make_unique<Listeners>() }
{
auto insert_it(std::end( *m_listeners ));
for (const auto &et : eventTypes)
{
insert_it = m_listeners->insert( insert_it, {et, {}} );
}
}
int main()
{
std::list<std::string> events { "Open", "Move", "Close" };
EventManager eManager{ events };
eManager.print();
}
#include <iostream>
#include <cmath>
using namespace std;
int fb(const int& b)
{
return pow(4 * b, 1 / 3);
}
int fax(const int& a, const int& x)
{
return (a + 2 * x) / pow(x, 1 / 2);
}
int main()
{
const int a = -18;
int resultAX = 0;
int resultB = 0;
for(int x = -1; x <= 10; ++x)
{
resultAX += fax(a, x);
}
for(int b = 2; b <= 10; b += 2)
{
resultB += fb(b);
}
cout << "x = " << resultAX << '\n'
<< "b = " << resultB << '\n'
<< "y = " << resultAX - resultB << endl;
}
OUT:
x = -108
b = 5
y = -113
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
char buf[1500]{"HTTPS/1.1 200 OK\nContent-Type: \
application/json; charset=utf-8\n\
Content-Length: 48\
Connection: Close{\"DATA\": {\"ERROR\":\
\"ANY USER IS LOGIN\"}}"};
auto bs{ find(begin(buf) , end(buf) , '{' ) };
auto es{ find(rbegin(buf), rend(buf) , '}' ) };
copy(bs, es.base(), ostream_iterator<char>(cout));
}
{"DATA": {"ERROR": "ANY USER IS LOGIN"}}
class Point
{
double x_coord;
double y_coord;
public:
Point(double x = {}, double y = {}) : x_coord{x}, y_coord{y}
{
}
};
class Square
{
Point* center_pt;
double size;
public:
Square(double x = {}, double y = {}, double sz = {1})
: center_pt{new Point(x / sz, y / sz)}, size{sz}
{
}
~Square(){ delete center_pt; }
};
int main()
{
Square sq(10, 10, 2);
Square sq2();
}
protected:
char* Str; // first
int Length; // second
String::String(const char* ptr)
: Str(new char[strlen(ptr) + 1]), Length(strlen(ptr) + 1)
{
strcpy(Str, ptr);
}
String::String(const String& t)
: Str(new char[t.Length]), Length(t.Length)
{
strcpy(Str, t.Str);
}
protected:
int Length; // first
char* Str; // second
только символы '0', '1', '2', '3', '4', '5','6', '7', '8', '9', 'A, 'B', 'C', 'D', 'E', 'F'.
//...
struct hex_str_traits
{
static bool is_hex_chars(const char* s)
{
auto size{ strlen(s) };
for (auto idx{ 0 }; idx < size; ++idx)
{
if (!in_range(s[idx], '0', '9') && !in_range(s[idx], 'A', 'F'))
return false;
}
return true;
}
static bool in_range(const int& value, const int& a, const int& b)
{
return a <= value && value <= b;
}
};
//...
class hex_string : public String
{
//...
public:
hex_string(const char* s);
//...
};
//...
hex_string::hex_string(const char* s)
: String( (hex_str_traits::is_hex_chars(s) ? s : "") )
{
}
//...
С чем это связано? Недоработка разработчиков?
#include<iostream>
#include<vector>
using namespace std;
template<typename T>
class Matrix
{
size_t cols;
size_t rows;
vector<vector<T>> m_matrix;
public:
Matrix(size_t c, size_t r) : cols{c}, rows{r}, m_matrix{}
{
m_matrix.reserve(cols);
for(auto i{0}; i < cols; ++i)
{
m_matrix.emplace_back(vector<T>(rows));
}
}
auto begin()
{
return m_matrix.begin();
}
auto end()
{
return m_matrix.end();
}
vector<T>& operator[](size_t i)
{
return m_matrix[i];
}
//...
};
int main()
{
Matrix<int> m(10,10);
m[5][5] = 5;
for(auto& c : m)
{
for(auto& e : c)
{
cout << e << ' ';
}
cout << endl;
}
return 0;
}
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 5 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using std::ofstream;
using std::vector;
using std::string;
using std::cout;
using std::cin;
using std::endl;
class Sport
{
vector<Sport> Sp;
char team;
double bal;
unsigned mesto;
string name;
public:
Sport(string _name = {}, char _team = {}, double _bal = {},
unsigned _mesto = {}, vector<Sport> sp = {})
: name {_name}, team{_team}, bal{_bal},
mesto{_mesto}, Sp{sp} {}
private:
friend ofstream& operator<<(ofstream& ofs, Sport& sp);
};
ofstream& operator<<(ofstream& ofs, Sport& sp)
{
ofs << sp.name << ' ';
ofs << sp.team << ' ';
ofs << sp.mesto << ' ';
ofs << sp.bal << ' ';
ofs << endl;
for(auto& p : sp.Sp)
{
ofs << p;
}
return ofs;
}
void writeSportsTo(const char* filename, Sport& sp)
{
ofstream of(filename);
if(of)
{
of << sp;
of.close();
}
}
int main()
{
constexpr char* filename = "C:\\sports.txt";
Sport sp;
writeSportsTo(filename, sp);
return 0;
}
#pragma once
#include <iostream>
template<typename T>
class TypeSize
{
public:
TypeSize(T v) : value{ v } {};
~TypeSize() {};
void dataTypeSize();
protected:
T value;
};
template<typename T>
void TypeSize<T>::dataTypeSize()
{
std::cout << "size: " << sizeof(value) << std::endl;
}
#pragma once
#include <typeinfo> // std::typeid()
#include "TypeSize.h"
template<typename T>
class TypeInfo : public TypeSize<T>
{
public:
TypeInfo(T v) : TypeSize<T>(v) {};
~TypeInfo() {};
void showTypeInfo();
};
template<typename T>
void TypeInfo<T>::showTypeInfo()
{
std::cout << "type: " << typeid(this->value).name() << std::endl;
}
#include "TypeInfo.h"
int main()
{
int a{ 5 };
TypeInfo<int> infInt(a);
infInt.dataTypeSize();
infInt.showTypeInfo();
std::getchar();
}
const char* data;
while((data = ether.tcpReply()) != NULL)
{
Serial.println(static_cast<String>(data));
}