如果我有
vector<int> vec { 1, 2, 3, 4}
Run Code Online (Sandbox Code Playgroud)
如何使用
的std ::反向
把它变成
vec {2,1,3,4}
这里我有简单的代码,它适用于std :: vector,但不适用于std :: list.
是不是因为列表中的元素没有合拢?
编辑:
好的,将列表放入func的最佳方法是什么?将它转换为矢量?
例如,我将数据放入PolyPolyLine时需要它
#include <iostream>
#include <vector>
#include <list>
using namespace std;
vector<int> func(int* buf)
{
vector<int> t;
t.push_back(buf[0]);
t.push_back(buf[1]);
return t;
}
int main() {
list<int> ls;
vector<int> v;
ls.push_back(2);
ls.push_back(111111);
v.push_back(12);
v.push_back(11);
vector<int> t1= func(&v[0]);
vector<int> t2= func(&ls.front());
cout<<t1[0]<<t1[1];
cout<<t2[0]<<t2[1];
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在从字符串创建向量时遇到问题.有人可以解释下面4例之间的区别吗?根据cplusplus.com我的预期Case 1工作方式与Case 2出于同样奇怪的原因一样,但事实并非如此.
typedef std::vector<uint8_t> uint8vec_t;
std::string keySecret ()
{
return std::string ("SomeSecret");
}
Run Code Online (Sandbox Code Playgroud)
情况1
// throws std::bad_alloc
uint8vec_t vSecret (keySecret ().begin (), keySecret ().end ());
Run Code Online (Sandbox Code Playgroud)
案例2
// results in a vector with strange length
uint8vec_t vSecret (keySecret ().begin (), keySecret ().begin () + keySecret ().length ());
Run Code Online (Sandbox Code Playgroud)
案例3
// throws std::bad_alloc
uint8vec_t vSecret (&keySecret ()[0], &keySecret ()[keySecret ().length ()]);
Run Code Online (Sandbox Code Playgroud)
案例4
// throws std::bad_alloc
uint8vec_t vSecret (&keySecret ()[0], &keySecret ()[0] + keySecret ().length ());
Run Code Online (Sandbox Code Playgroud) 我有一个TIVA-C微控制器项目,该项目使用arm-none-eabi-gcc进行了编译,尽管我添加了string.h,但是却收到“未定义对strcmp的引用”链接器错误。我正在使用预编译的工具链:gcc-arm-none-eabi-4_8-2014q3-20140805-linux.tar.bz2从这里:https ://launchpad.net/gcc-arm-embedded/+download 。我的makefile切换:
# define flags
CFLAGS = -g -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp
CFLAGS +=-Os -ffunction-sections -fdata-sections -MD -std=c99 -Wall
CFLAGS += -pedantic -DPART_$(MCU) -c -I$(TIVAWARE_PATH)
CFLAGS += -DTARGET_IS_BLIZZARD_RA1
LDFLAGS = -T $(LD_SCRIPT) --entry ResetISR --gc-sections
Run Code Online (Sandbox Code Playgroud)
还有其他人也有同样的问题,但是他们在LDFLAGS中打开了-nostd,而我显然没有。我现在没主意了,所以任何提示都会很棒。
看看下面的代码:
#include <algorithm>
#include <deque>
#include <iostream>
using namespace std;
int main()
{
deque<int> in {1,2,3};
deque<int> out;
// line in question
move(in.begin(), in.end(), out.begin());
for(auto i : out)
cout << i << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这不会移动任何东西.看一下这里的例子,必须写下这样的行:
move(in.begin(), in.end(), std::back_inserter(out));
Run Code Online (Sandbox Code Playgroud)
这在某种程度上是有道理的,因为std :: move期望它的前两个参数是InputInterators(这里满足),第三个参数是OutputIterator(out.begin()不是).
如果原始代码被执行并且move被传递给不是OutputIterator的迭代器,那么实际会发生什么?为什么C++的类型安全在这里不起作用?为什么输出迭代器的构造委托给外部函数,即为什么不out.backInserter()存在?
#include <thread>
#include <iostream>
using namespace std;
class A
{
public:
A(){}
void m(std::string* s)
{
cout<<*s;
}
void m(std::string s)
{
cout<<s;
}
};
int main()
{
A a;
string str="hi!\n";
std::thread(&A::m,a,&str);
}
Run Code Online (Sandbox Code Playgroud)
这不编译; 它给:
error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, A&, std::string*)’
Run Code Online (Sandbox Code Playgroud)
如果我删除第二个成员,它编译!为什么?我不能在std :: thread中使用重载方法?
我知道如果你想为C++11你的Qt代码添加功能,你需要在.pro文件中写下这一行:
QMAKE_CXXFLAGS += -std=c++11,
Run Code Online (Sandbox Code Playgroud)
但你也可以用另一条代替它:
CONFIG+=c++11.
Run Code Online (Sandbox Code Playgroud)
问题是:什么更好用?
我想对任意类型的向量进行排序,因此我编写了以下代码:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template<class T>
bool compare(T a, T b) {
return a < b;
}
int main() {
vector<int> v;
v.push_back(3);
v.push_back(4);
v.push_back(2);
v.push_back(1);
sort(v.begin(), v.end(), compare);
for (size_t i = 0; i < v.size(); i++) {
cout << v.at(i) << " ";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码未编译,错误消息如下:
..\src\Test.cpp:22:34: error: no matching function for call to 'sort(std::vector<int>::iterator, std::vector<int>::iterator, <unresolved overloaded function type>)'
..\src\Test.cpp:22:34: note: candidates are:
... and more
Run Code Online (Sandbox Code Playgroud)
当我使用具体类型实现compare-function时,它可以工作.有人可以告诉我如何使用模板比较功能吗?
绑定到作为局部变量的值是否是一种好习惯:
bool func(const std::string& a, const std::string& b)
{
return a + "abc" == b;
}
Run Code Online (Sandbox Code Playgroud)
......
auto str = getRandomString();
auto it = std::find_if(
vec.begin(),
vec.end(),
std::bind(func, str, std::placeholders::_1));
Run Code Online (Sandbox Code Playgroud)
作为std::bind函数对象的返回值,它为每个局部变量创建一个新的函数对象吗?我应该改用lambda表达式吗?
auto it = std::find_if(
vec.begin(),
vec.end(),
[str](const std::string& b){return str + "abc" == b;});
Run Code Online (Sandbox Code Playgroud) 我试图std::hash<T>通过提供专业化来扩展const char,以便我可以const char*用作键类型std::unordered_map.
这是我试过的:
#include <unordered_map>
#include <functional>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
namespace std
{
template<>
struct hash<const char*>
{
size_t operator()(const char* const& s) const
{
size_t h = 0;
const char* tmp = s;
while (*tmp)
h = (h << 5) + h + (unsigned char)toupper(*tmp++);
printf("hash of '%s' is (%ld)\n", s, h);
return h;
}
};
}
int main(int argc, char* argv[])
{
const char* name1= "Mark …Run Code Online (Sandbox Code Playgroud)