标签: std

为什么C++标准算法"count"会返回difference_type而不是size_t?

为什么的返回类型std::countdifference_type迭代器(通常是的ptrdiff_t).

由于计数永远不会是负数,size_t 技术上不是正确的选择吗?如果计数超出ptrdiff_t阵列理论上可能的大小范围,该size_t怎么办?


编辑:到目前为止,没有合适的答案为什么函数返回ptrdiff_t.从下面的答案中得到的一些解释是返回类型iterator_traits<InputIterator>::difference_type是通用的,可以是任何东西.直到那时它才有意义.有些情况下,计数可能会超过size_t.但是,为什么返回类型是typedef ptrdiff_t iterator_traits<InputIterator>::difference_type标准迭代器而不是为什么仍然没有意义typedef size_t iterator_traits<InputIterator>::difference_type.

c++ std

38
推荐指数
4
解决办法
3103
查看次数

如何获取所有Python标准库模块的列表

我想要sys.builtin_module_names除了标准库以外的东西.其他不起作用的东西:

  • sys.modules - 仅显示已加载的模块
  • sys.prefix - 一个包含非标准库模块EDIT的路径:并且似乎不能在virtualenv中工作.

我想要这个列表的原因是我可以将它传递给http://docs.python.org/library/trace.html--ignore-module--ignore-dir命令行选项trace

所以最终,我想知道如何在使用trace或时忽略所有标准库模块sys.settrace.

编辑:我希望它在virtualenv中工作.http://pypi.python.org/pypi/virtualenv

编辑2:我希望它适用于所有环境(即跨越操作系统,在virtualenv的内部和外部.)

python std virtualenv

37
推荐指数
6
解决办法
9852
查看次数

C++ std :: set线程安全吗?

我有一个关于std :: set的线程安全性的问题.

据我所知,我可以遍历一个集合并添加/擦除成员,这不会使迭代器无效.

但请考虑以下情况:

  • 线程'A'遍历一组shared_ptr <Type>
  • 线程'B'偶尔会将项目添加到此集合中.

程序运行时我经历过段错误,我不确定为什么会这样.缺乏线程安全的原因是什么?

c++ stl std stdset

36
推荐指数
3
解决办法
3万
查看次数

是否为C++标准允许的自定义类型的std :: to_string专门化?

在C++ 11及更高版本中,是否允许专用std::to_stringstd自定义类型的命名空间?

namespace std {
string to_string(::MyClass const & c) { return c.toString(); }
}
Run Code Online (Sandbox Code Playgroud)

示例用例:

int main() {
    MyClass c;
    std::cout << std::to_string(c) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

c++ std tostring specialization c++11

36
推荐指数
4
解决办法
4630
查看次数

std :: vector或boost :: vector线程安全吗?

我有多个线程同时调用push_back()共享对象std::vector.是std::vector线程安全的?或者我是否需要自己实现该机制以使其线程安全?
我想避免做额外的"锁定和释放"工作,因为我是图书馆用户而不是图书馆设计师.我希望寻找现有的矢量线程安全解决方案.怎么样boost::vector,这是从1.48.0以后新推出的.它是线程安全的吗?

c++ boost stl std thread-safety

35
推荐指数
3
解决办法
4万
查看次数

std :: ofstream,在写入之前检查文件是否存在

我正在使用C++ 在Qt应用程序中实现文件保存功能.

我正在寻找一种方法来检查所选文件是否已经存在,然后写入它,以便我可以向用户提示警告.

我正在使用std::ofstream,我不是在寻找Boost解决方案.

c++ fstream std stream ofstream

34
推荐指数
5
解决办法
7万
查看次数

按对象属性搜索对象矢量

我试图找出一种很好的方法来找到向量中某个对象的索引 - 通过将字符串与对象中的成员字段进行比较.

find(vector.begin(), vector.end(), [object where obj.getName() == myString])
Run Code Online (Sandbox Code Playgroud)

我搜索没有成功 - 也许我不完全明白要寻找什么.

c++ gcc stl std

34
推荐指数
2
解决办法
4万
查看次数

在macOS High Sierra 10.13上破解了c ++ std库

我最近买了一台新的MacBook,我转移了我的旧会话.从那时起,在我升级到10.13之后,我无法得到任何包括iostream在内的任何东西.
鉴于此计划:

#include <iostream>

int main(void)
{
    std::cout << "Hello world !" << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在以下时间给出我的输出g++ main.cpp:

In file included from main.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string:470:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/string_view:171:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__string:56:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:640:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:629:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/typeinfo:61:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/exception:82:
In file included from …
Run Code Online (Sandbox Code Playgroud)

c++ macos compilation std macos-high-sierra

34
推荐指数
3
解决办法
1万
查看次数

哪个std :: sync :: atomic :: Ordering使用?

所有std::sync::atomic::AtomicBool采用内存排序的方法(Relaxed,Release,Acquire,AcqRel和SeqCst),我以前没用过.在什么情况下应该使用这些值?该文档使用令人困惑的"加载"和"存储"术语,我并不理解.例如:

生成器线程改变由a持有的某个状态Mutex,然后调用AtomicBool: compare_and_swap(false, true, ordering):(以合并失效),如果它交换,则将"无效"消息发布到并发队列(例如,mpsc或winapi PostMessage).消费者线程重置AtomicBool,从队列中读取,并读取互斥锁持有的状态.生产者是否可以使用轻松排序,因为它之前是互斥锁,或者必须使用Release?消费者可以使用store(false, Relaxed)或必须使用它compare_and_swap(true, false, Acquire)来接收来自互斥锁的更改吗?

如果生产者和消费者共享一个RefCell而不是一个Mutex

sync atomic std rust

33
推荐指数
1
解决办法
3476
查看次数

C++ STL map:是访问时间O(1)?

关键是在std::mapO(1)上查找?我以为直到我想到它为止.它基于树实现,因此查找时间应为O(log N),对吗?

而且,是否有可能让O(1)查找字符串键std::unordered_map

c++ std data-structures c++11

32
推荐指数
2
解决办法
3万
查看次数