
C++
- 8 ответов
- 0 вопросов
7
Вклад в тег
#include <iostream>
#include <ctime>
void fillRandom(int* arr, int size, int min, int max) {
for(int i{0}; i < size; ++i) {
arr[i] = min + (rand() % ((max - min) + 1));
}
}
int main() {
srand(time(NULL));
int len{10}, min{10}, max{99};
int array[len];
fillRandom(array, len, min, max);
for(int& i : array) std::cout << i <<' ';
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
void fill_random(std::vector& v, int min, int max) {
int i{0};
auto lambda = [&](int index){ v[i] = min + (rand() % ((max - min) + 1)); ++i; };
std::for_each(v.begin(), v.end(), lambda);
}
int main() {
srand(time(NULL));
int size{10}, min{10}, max{99};
std::vector<int> vec(size);
fill_random(vec, min, max);
for(int& i : vec) std::cout << i << ' ';
return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main() {
std::string str{"This string in file"};
std::stringstream s(str);
std::vector<std::string> vec;
std::string word{""};
while(s >> word) vec.push_back(word);
for(auto &i : vec) std::cout << i << '\n';
return 0;
}
#include <iostream>
#include <regex>
#include <string>
#include <vector>
int main() {
std::string str{"Hello! This string of your file"};
std::regex reg{"\\S+"};
std::vector<std::string> vec;
std::smatch sm;
while(std::regex_search(str, sm, reg)) {
vec.push_back(sm.str());
str = sm.suffix();
}
for(auto &i : vec) std::cout << i <<' \n';
return 0;
}
#include <iostream>
#include <string>
#include <vector>
void split(std::string& str, std::vector<std::string>& vec) {
std::string tmp{""}, delimiter{".!?"};
int pos{0};
for(int i{0}; i < str.length(); ++i) {
tmp += str[i];
pos = delimiter.find(str[i]);
if(pos > -1) {
vec.push_back(tmp);
tmp = "";
while(str[i+1] == ' ') ++i;
}
}
}
int main() {
std::string s{"Hi bro! How are you? This your string of file."};
std::vector<std::string> res;
split(s, res);
for(auto& el : res) std::cout << el << '\n';
return 0;
}
#include <iostream>
template <typename T>
void commonElem(T a[], T a2[], T a3[], int len, int len2, int len3) {
std::cout<<"Common elements arrays is: ";
for(int i{0}; i < len; ++i) {
bool f2{false}, f3{false};
for(int j{0}; j < len2; ++j) if(a[i] == a2[j]) f2 = true;
for(int k{0}; k < len3; ++k) if(a[i] == a3[k]) f3 = true;
if(f2 && f3) std::cout << a[i] << ' ';
}
}
int main() {
int arr[]{23,12,54,2,7}, arr2[]{2,23,1,65}, arr3[]{43,2,76,4,23,8,96};
int l = std::size(arr), l2 = std::size(arr2), l3 = std::size(arr3);
commonElem(arr, arr2, arr3, l, l2, l3);
return 0;
}