我是c ++正则表达式的新手.我有一个字符串"{1,2,3}",我想提取数字1 2 3.我认为我应该使用regex_search但它失败了.
#include<iostream>
#include<regex>
#include<string>
using namespace std;
int main()
{
string s1("{1,2,3}");
string s2("{}");
smatch sm;
regex e(R"(\d+)");
cout << s1 << endl;
if (regex_search(s1,sm,e)){
cout << "size: " << sm.size() << endl;
for (int i = 0 ; i < sm.size(); ++i){
cout << "the " << i+1 << "th match" <<": "<< sm[i] << endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
{1,2,3}
size: 1
the 1th match: 1
Run Code Online (Sandbox Code Playgroud) 如何在一个类中存储类的对象unordered_set?我的程序需要经常检查对象是否存在,如果存在unordered_set,则对该对象进行一些更新.
我已经在线查看了如何使用unordered_set,但遗憾的是大多数教程都是关于使用它int或string类型.但是我如何在课堂上使用它呢?我怎样才能定义一个哈希函数来使node_id下面的例子成为关键的unordered_set?
#include <iostream>
#include <unordered_set>
using namespace std;
// How can I define a hash function that makes 'node' use 'node_id' as key?
struct node
{
string node_id;
double value;
node(string id, double val) : node_id(id), value(val) {}
};
int main()
{
unordered_set<node> set;
set.insert(node("1001", 100));
if(set.find("1001") != set.end()) cout << "1001 found" << endl;
}
Run Code Online (Sandbox Code Playgroud) Shape_area是什么意思?我注意到它不是Aland和Awater的总和.这三个单位是多少?
我写了这个函数,我设置了线宽来绘制一个矩形,但是在调用它时,线宽根本不会改变.我怎样才能glLineWidth正确使用?
void drawRect(Rectangle &rect)
{
double x1 = rect.min.x;
double y1 = rect.min.y;
double x2 = rect.max.x;
double y2 = rect.max.y;
glLineWidth(3.0f);
glBegin(GL_LINE_LOOP);
glVertex2d(x1, y1);
glVertex2d(x2, y1);
glVertex2d(x2, y2);
glVertex2d(x1, y2);
glEnd();
}
Run Code Online (Sandbox Code Playgroud) 我期望下面的代码应该打印不同的时间戳t1和t2,但结果显示t1和t2是相同的.我在哪里弄错了?
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
time_t t1 = time(NULL);
cout << "time now " << ctime(&t1) << endl;
time_t t2 = t1 + 10000.0;
cout << "time now " << ctime(&t1) << endl << " time later " << ctime(&t2) <<endl;
}
Run Code Online (Sandbox Code Playgroud)
结果:
time now Thu Apr 28 20:37:03 2016
time now Thu Apr 28 20:37:03 2016
time later Thu Apr 28 20:37:03 2016
Run Code Online (Sandbox Code Playgroud) 我真的很困惑这种类型的指针定义:
char *notes[] = {"Ab", "F#", "B", "Gb", "D"};`.
Run Code Online (Sandbox Code Playgroud)
我理解notes这里是一个指向char的指针数组,我理解为注释'元素应该都是char类型变量的地址.我哪里错了?那么这是如何工作的呢?
#include<iostream>
#include<string>
using namespace std;
int main()
{
char *notes[] = {"Ab", "F#", "B", "Gb", "D"};
cout << *(char**)(notes+2);
}
Run Code Online (Sandbox Code Playgroud)
char**那里的演员是什么?它的意义是什么?
我有一个存储{1,2,3,4,5}的向量.我试图打印*(vec.end())并取回结果6.我不知道如何解释这个.同样,调用vec.find(500)给出了结果6.为什么我得到这个数字?
#include<iostream>
#include<iterator>
#include<set>
#include<map>
int main()
{
int a[] = {1,2,3,4,5};
std::set<int> set1(a,a+sizeof(a)/sizeof(int));
for (std::set<int>::iterator itr=set1.begin();itr!=set1.end();++itr){
std::cout << *itr << std::endl;
}
//std::pair<std::set<int>::iterator, bool> ret;
//ret = set1.insert(1);
//std::cout << *(ret.first) << "first;second" << ret.second << std::endl;
std::set<int>::iterator itr1 = set1.begin();
set1.insert(itr1,100);
std::advance(itr1,3);
std::cout << *itr1 << std::endl;
std::cout << *(set1.find(500)) << std::endl;
std::cout << *(set1.end()) << std::endl;
}
Run Code Online (Sandbox Code Playgroud) 我总是对为优先级队列容器定义比较器感到困惑,并且不知道如何理解它.例如,我有一个vector的pair<int,int>,在这里我想通过它的第二个字段值对递减排序.
所以代码看起来像这样:
struct Compare
{
bool operator()(pair<int,int> const &p1, pair<int,int> const &p2) const
{
return p1.second < p2.second;
}
};
priority_queue<pair<int,int>,vector<pair<int,int> >, Compare> pqueue;
Run Code Online (Sandbox Code Playgroud)
如何理解"<"这里的操作员,因为我认为它应该是">"第一次,并且必须根据结果进行更改.为什么"<"降序而不是">"?我只想在下次使用时第一次拍摄时才能正确使用priority_queue.谢谢.
例如,我的表中的一列是一个数组,我想检查该列是否包含一个包含子字符串"denied"的元素(所以像"在12:00 pm拒绝","被admin拒绝"这样的元素都将被计算,我相信我将不得不使用"喜欢"来识别模式.如何为此编写sql?