как правильно удалять произвольные элементы из вектора
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v{1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 9, 0};
v.erase(remove(v.begin(), v.end(), 0), v.end());
// или
// auto it = stable_partition(v.begin(), v.end(), [](int n){ return n != 0;}); // or partition
// v.erase(it, v.end());
for(int i : v)
{
cout << i << " ";
}
}
или никак и надо использовать другой контейнер?
Но если использовать std::copy, то работать будет всё точно так же:
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<wstring> words = { L"1", L"Goose", L"14", L"gas", L"/" , L"file10", L"file11" };
sort(words.begin(), words.end());
for(auto s : words)
{
wcout << s << " ";
}
wcout << endl;
wcin.get();
}
OUT: / 1 14 Goose file10 file11 gas
bool find(node* n, int value)
{
node* ptr = n;
while(ptr != nullptr)
{
if(ptr->value == value) return true;
ptr = ptr->next;
}
return false;
}
node* find(node* n, int value)
{
node* ptr = n;
while(ptr != nullptr)
{
if(ptr->value == value) return ptr;
ptr = ptr->next;
}
return ptr;
}
node* result = find(...);
if(result)
{
//...
}
What are the supported operating systems?
C:\Users\Angi\Desktop\DemoGame\x64\Debug\DemoGame.exe
CLR не позволит и MessageBox привычным способом вызвать.
#include<Windows.h>
using namespace System;
int main(array<System::String ^> ^args)
{
#pragma comment(lib, "User32.lib")
MessageBox(NULL, L"Hello World!", L"Hello World Box", 0);
return 0;
}
p.s. название библиотеки поменялось
g++ main.cpp -o main.exe -lglfw3 //Компиляция происходит без проблем, а линковка дает ошибки
#include "glfw3.h"
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
undefined reference to `_imp__glClear@4'
то opengl32.a надо закинуть в project\libsfilein.txt
Petya,K221,1,2,3;Vasya,K222,4,5,6;Senya,K223,7,8,0;Goga,K224,1,2,3;
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;
template<typename X = int>
struct Data
{
X one;
X two;
X three;
Data(X x1 = 0, X x2 = 0, X x3 = 0) : one{x1}, two{x2}, three{x3}
{}
};
template<typename X = int>
struct DataRecord
{
pair<string, string> text;
Data<X> data;
DataRecord(string _s1 = {}, string _s2 = {},
X _x1 = 0, X _x2 = 0, X _x3 = 0)
: text{_s1, _s2}, data{_x1, _x2, _x3}
{}
};
template<typename X = int>
istream& operator>>(istream& is, DataRecord<X>& record)
{
char c;
getline(is, record.text.first, ',');
getline(is, record.text.second, ',');
is >> record.data.one;
is >> c;
if(c == ',')
{
is >> record.data.two;
is >> c;
if(c == ',')
{
is >> record.data.three;
}
}
is >> c;
if(c != ';')
{
is.clear(ios_base::failbit);
}
return is;
}
struct DataSet
{
vector<DataRecord<>> records;
DataSet() : records{}{}
void load(string filename) noexcept;
Data<> sumsByColumns() noexcept;
};
void DataSet::load(string filename) noexcept
{
ifstream in(filename);
if(!in)
{
//...
return;
}
copy(istream_iterator<DataRecord<>>{in}, {}, back_inserter(records));
}
Data<> DataSet::sumsByColumns() noexcept
{
Data<> result;
for(const auto& rec : records)
{
result.one += rec.data.one;
result.two += rec.data.two;
result.three += rec.data.three;
}
return result;
}
int main()
{
DataSet ds;
ds.load("C:\\infile.txt");
Data<> result{ds.sumsByColumns()};
for(const auto& rec : ds.records)
{
cout << rec.text.first << '\t'
<< rec.text.second << '\t'
<< rec.data.one << '\t'
<< rec.data.two << '\t'
<< rec.data.three << '\n';// Если формат записей в файле
// построчный '\n' нужно убрать.
}
cout << "\nTotal:\t\t"
<< result.one << '\t'
<< result.two << '\t'
<< result.three << endl;
}
OUT
Petya K221 1 2 3
Vasya K222 4 5 6
Senya K223 7 8 0
Goga K224 1 2 3
Total: 13 17 12
map<Point, bool> и map<Point, long>
bool operator<(const Point& p1, const Point& p2)
{
return (p1.x0 < p2.x0) && (p1.y0 < p2.y0);
}
bool operator<(const Point& a, const Point& b)
{
return (a.x < b.x) || (a.y < b.y);
}
name1.compare(name2); //strcmp()
name1 == name2; //operator==
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
string s = "10 20 30 50 99 786 521 3";
istringstream is(s);
vector<int> c;
copy(istream_iterator<int>(is), {}, back_inserter(c));
for(int i : c)
{
cout << i << ' ';
}
int j = c[1] + c[2];
cout << "j == " << j;
}