我计划在我的代码中使用 std::list ,我决定不使用 std::forward_list ,因为对于删除(我认为),必须遍历整个列表, std::forward_list 的复杂度为 O(N) (正在单个链接列表)。然而,当我查看文档时,我注意到两个 stl 容器都具有 O(N) 复杂度来删除项目。
经过一番思考,我明白了原因(我认为)。这是因为在这两种情况下,都必须扫描整个列表以首先找到节点,然后删除它。这是正确的吗?
然后我研究了“erase”和“erase_after”方法,它们的复杂性是“与擦除(破坏)的元素数量成线性关系”。这是因为,我将迭代器传递给节点(有点像“指针”)。但是,我不能(或不愿意)在代码中传递此迭代器来访问节点中的数据。我不确定如果列表被修改,这个迭代器是否有效?想法?
我的问题是,有没有办法获得指向列表中节点的指针。这样,我知道它将在我的程序的整个生命周期内有效,并将其传递。我只需查看它即可访问我的数据。
在这里通过函数包装器浏览Boost库中的一个教程时,我遇到了以下代码:
1 boost::function<float (int x, int y)> f;
2
3 struct int_div {
4 float operator() (int x, int y) const { return ((float)x)/y; }
5 };
6
7
8 int main()
9 {
10 f = int_div();
11 cout << f(5, 3) << endl;
12 return 0;
13 }
Run Code Online (Sandbox Code Playgroud)
我试图围绕operator()在struct中定义一个function(),然后将struct(using ())分配给函数包装器f.在第3-5和第10行中,有人可以帮助我了解正在发生的事情.
我想在 wiki 页面的表中创建下表:
A : B (Line 1)
C : D
E : F1 G1 (Line 3)
F2 G2
F3 G3
H : I
Run Code Online (Sandbox Code Playgroud)
一种选择是创建包含四列的主表。这意味着为第 3 列创建空字符串,为第 1 行(以及类似的行)创建空字符串。有没有
{|
|A
|:
|B
|
|
|-
(etc)
|}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种方法可以表明我想为主表的第 3 行/第 3 列元素创建一个嵌入表?
我尝试了以下方法:
{|
{|
|A
|:
|B
|-
|C
|:
|D
|-
|E
|:
{|
|F1
|G1
|-
|F2
|G2
|-
|F3
|G3
|}
|-
|H
|:
|I
|}
Run Code Online (Sandbox Code Playgroud)
然而上面的方法不起作用,“B”和“D”缩进到“F/G”表的右侧。
有任何想法吗?
一种相关的问题,如何转义字符单个字符,如“{”和“|” 所以它们会被显示。
我有以下代码:
enum class State : uint32_t
{
FREE,
IDLE,
COAST,
MOVE,
STOP
};
std::atomic<State> car1_state = State::IDLE; <--- Line a
std::atomic<State> car2_state(State::IDLE); <--- Line b
Run Code Online (Sandbox Code Playgroud)
以下是原子头文件的片段:
// c++ header file - atomic
template<typename _Tp>
struct atomic
{
private:
_Tp _M_i;
public:
atomic() noexcept = default;
~atomic() noexcept = default;
atomic(const atomic&) = delete; <--- Line c
atomic& operator=(const atomic&) = delete;
atomic& operator=(const atomic&) volatile = delete;
constexpr atomic(_Tp __i) noexcept : _M_i(__i) { } <--- Line d
operator _Tp() …Run Code Online (Sandbox Code Playgroud) 我有以下示例代码来解释我的问题.根据STD地图容器文档(http://www.cplusplus.com/reference/map/map/operator%5B%5D/),operator [](或"at"方法)返回对映射值的引用.我明白为什么第13行编译并正常工作(当我将元素插入vec1时,映射中的映射值会更新).我不明白为什么第13行不会导致编译错误,因为vec1不是引用而operator []返回引用.
1 #include <map>
2 #include <vector>
3
4 using namespace std;
5
6 int main()
7 {
8 map<int, vector<int> > port;
9
10 port[1] = vector<int>(1, 10);
11
12 vector<int> &vec1 = port[1]; // <===
13 vector<int> vec2 = port[1]; // <===
14
15 return 0;
16 }
Run Code Online (Sandbox Code Playgroud)
我想也许operator []的实际实现被重载以返回两种类型(值和引用).但是,当我查看"map"头文件时,它似乎没有(除非我遗漏了一些东西):
文件:/usr/include/c++/4.7/profile/map.h
// 23.3.1.2 element access:
mapped_type&
operator[](const key_type& __k)
{
__profcxx_map_to_unordered_map_find(this, size());
return _Base::operator[](__k);
}
#ifdef __GXX_EXPERIMENTAL_CXX0X__
mapped_type&
operator[](key_type&& __k)
{
__profcxx_map_to_unordered_map_find(this, size());
return _Base::operator[](std::move(__k)); …Run Code Online (Sandbox Code Playgroud) 我有一个关于以下关于重载的示例程序的问题 - >运算符,在C++教程中是相同的:
5 class myclass
6 {
7 public:
8 int i;
9
10 myclass *operator->()
11 {return this;}
12 };
13
14 int main()
15 {
16 myclass ob;
17
18 ob->i = 10;
19 cout << ob.i << " " << ob->i << endl;
20
21 return 0;
22 }
$ ./a.out
10 10
Run Code Online (Sandbox Code Playgroud)
我试图了解第18行是如何工作的.我理解"ob"不是一个指针,但由于"class myclass"定义了运算符" - >","ob-> i"是有效的(语法),到目前为止还不错.但是,"ob->"返回一个指针,我看不到如何取消引用它来访问成员"i"并进行设置.
我假设上面的解释也将解释第19行"ob-> i"是如何打印为int的.
艾哈迈德,谢谢你.
我正在使用Postman发送AWS S3 RestAPI"获取存储桶(版本2)"以获取存储桶列表.
存储桶的名称是"test-bucket-1.ahadomain.com"(ahadomain.com是我在aws中命名存储桶时使用的虚拟不存在的域).我使用的用户凭据具有进行S3调用的所有权限.我正在关注页面上的信息 - http://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html
我正在使用端点: https ://test-bucket-1.s3.us-east-1.amazonaws.com我发送以下标题:内容类型,主机,X-Amz-Content-Sha256,X- Amz-Date,授权
我是否需要添加"list-type"作为查询参数或标题?如果作为查询参数,我如何在url中声明它.
我收到以下响应,其中不包含内容列表,只是有关存储桶本身的信息:
<?xml version="1.0" encoding="UTF-8"?>
<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Owner>
<ID>6893100ea2b48696e8ccc3aa17414f4325cf59b574474ad9de0bcb0d139590c9</ID>
<DisplayName>ahmedsmail</DisplayName>
</Owner>
<Buckets>
<Bucket>
<Name>test-bucket-1.ahadomain.com</Name>
<CreationDate>2017-09-06T06:36:15.000Z</CreationDate>
</Bucket>
</Buckets>
</ListAllMyBucketsResult>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
艾哈迈德,谢谢你.
我想对一个数组进行排序int32_t,这样所有正数(包括零)首先出现,然后是所有负数.输出中+ ve和-ve数的相对顺序应与输入的顺序相同.例如
Input : 9, 4, -2, -1, 5, 0, -5, -3, 2 <br>
Expected Output : 9, 4, 5, 0, 2, -2, -1, -5, -3
Run Code Online (Sandbox Code Playgroud)
但是,我得到以下输出:
Actual output : 2 0 5 4 9 -3 -5 -1 -2 <br>
Run Code Online (Sandbox Code Playgroud)
输出部分准确,+ ve然后-ve,但+ ve和-ve数列表相反.有人可以请帮助.
#include <iostream>
#include <algorithm>
using namespace std;
bool comp(int32_t lhs, int32_t rhs)
{
if ( (lhs < 0) && (rhs >= 0) )
return false;
else
return true;
}
int main()
{
vector<int32_t> input{9, 4, …Run Code Online (Sandbox Code Playgroud) 我已经阅读了很多关于为什么在C++等中不能添加两个字符串文字的帖子,以及operator + support将字符串文字添加到整数.
但是,我试图理解以下代码中的编译器错误:
string str1, str2, str3;
int i = 10;
str1 = "Hello " + i;
str2 = i + "Mars";
str3 = "Hello " + i + "Mars";
Run Code Online (Sandbox Code Playgroud)
初始化str1和str2工作正常,但构造str3给出以下错误:
example.cpp:在函数中
int main():
example.cpp:20:27:错误:类型const char*和const char [5]二进制的操作数无效operator+
Q1:在错误消息中,我理解const char [5]是指"Mars".是什么意思const char*,i转换成后的整数char *?
Q2:operator+有从左到右的关联性,我猜测的构造str3可以写成:
str3 = ("Hello " + i) + "Mars";
Run Code Online (Sandbox Code Playgroud)
不 …
我有以下示例C ++程序,它删除了指向类中方法的指针。调用该方法时出现编译器错误。
10 class Foo
11 {
12 void set_parm1(string) {}
13 void set_parm2(string) {}
14
15 typedef void (Foo::*set_func_t)(string);
16 struct Bar
17 {
18 string value;
19 set_func_t set_func;
20 };
21
22 map<string, Bar> parameter;
23
24 public:
25 Foo();
26 void set_value(string key, string value);
27 };
28
29 Foo::Foo()
30 {
31 Bar temp;
32
33 temp.value = "value1";
34 temp.set_func = &Foo::set_parm1;
35 parameter["param1"] = temp;
36
37 temp.value = "value2";
38 temp.set_func = …Run Code Online (Sandbox Code Playgroud)