我的代码中有这个.
vector<unsigned int> import;
vector<unsigned int> aquired;
. . .
size_t new_size = max(import.size(),aquired.size());
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
error: more than one instance of
overloaded function "max" matches the
argument list:
function "max(int, int)"
function "max(unsigned int, unsigned int)"
function "max(int, unsigned int)"
function "max(unsigned int, int)"
function "max(long long, long long)"
function "max(unsigned long long, unsigned long long)"
function "max(long long, unsigned long long)"
function "max(unsigned long long, long long)"
function "max(float, float)"
function "max(double, double)"
function "max(float, double)"
function "max(double, float)" …Run Code Online (Sandbox Code Playgroud) 我有以下问题:
void add(){
cRow Row();
Row.add("Column", "Value");
std::vector<cRow> mRows;
mRows.push_back(Row);
}
cRow::cRow(): mCol(NULL), mVal(NULL) {
}
cRow::add(const char* Col, const char* Val){
mCol = strdup(Col);
mVal = strdup(Val);
}
cRow::~cRow(){
free(mCol);
free(mVal);
}
Run Code Online (Sandbox Code Playgroud)
将局部变量Row添加到向量后,将为该行调用析构函数并释放字符串.
显然,现在也释放了向量中存储行的字符串的指针.离开本地范围后对行的任何访问都将导致段错误.
行的转储看起来像这样的2个调用:
| (null) | (null) |
-----------------------------------------------------
| (null)| (null) |
| LastContainerUpdatePropagation| 1307967498 |
------------------------ END ------------------------
Run Code Online (Sandbox Code Playgroud)
3次通话后:
| (null) | (null) |
-----------------------------------------------------
| (null)| (null) |
| (null)| (null) |
| LastSystemUpdatePropagation| 1307967498 |
------------------------ END ------------------------
Run Code Online (Sandbox Code Playgroud)
并且在完全离开范围而不添加新行之后,每一行都被释放.
那么,现在我的问题是:std:vector如何复制对象?我需要做些什么来保持指向字符串的指针或将它们复制到另一个空间?
非常感谢你!
For循环应该遍历std :: vector并填充内容.
First for循环给出了一条错误消息:
没有找到二元运算符<<不可转换
vector<MyClass>classVector;
for (vector<MyClass>::iterator i = classVector.begin();
i != classVector.end();
++i)
{
cout << *i << endl;
}
Run Code Online (Sandbox Code Playgroud)
MyClass.h:
class MyClass{
private:
string newTodayTaskString;
public:
MyClass(string t) : newTodayTaskString (t){}
~MyClass(){}
};
Run Code Online (Sandbox Code Playgroud)
这个for循环遍历字符串向量并且完美地工作.为什么?
vector<string>stringVector;
for (vector<string>::iterator i = stringVector.begin();
i != stringVector.end();
++i)
{
cout<<*i<<endl;
}
Run Code Online (Sandbox Code Playgroud) 我刚开始使用Vectors进行编码并编写了以下基本代码.
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> second(4,100);
cout<<second[0]<<endl;
cout<<second[1]<<endl;
cout<<second[2]<<endl;
cout<<second[3]<<endl;
cout<<second[4]<<endl;
cout<<second[5]<<endl;
cout<<second[6]<<endl;
cout<<second[7]<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出如下
100
100
100
100
0
135145
0
0
RUN FINISHED; exit value 0; real time: 10ms; user: 0ms; system: 0ms
Run Code Online (Sandbox Code Playgroud)
为什么cout<<second[5]不同于其他指数的输出大于3.任何帮助表示赞赏.
我发现自己经常处于编写以下代码的情况:
std::map<int, std::vector<int>> dict;
void insert(int key, int val) {
if (dict.find(key) == dict.end()) {
dict[key] = std::vector<int>();
}
dict[key].push_back(val)
}
Run Code Online (Sandbox Code Playgroud)
编写这个插入函数是否有一种不那么冗长的方式(在C++ 11中)?
我的代码必须在几个平台(Linux,Mac,Windows)和架构(32/64位)上运行,并有几个编译器(GCC,MSVC,Intel).在瓶颈代码中,我已经分配std::vector<double> x了大小> 0.用给定的常量值填充它的最佳方法是什么c?
我的第一种方法是循环:
for(std::size_t i = 0; i < x.size(); ++i)
x[i] = c;
Run Code Online (Sandbox Code Playgroud)
循环也可以用迭代器完成:
for(std::vector<double>::iterator it = x.begin(); it != x.end(); ++it)
*it = c;
Run Code Online (Sandbox Code Playgroud)
我也可以使用类提供的赋值函数,std::vector并且如果大小相同,我可以确信没有重新分配(我希望):
x.assign(x.size(), c);
Run Code Online (Sandbox Code Playgroud)
有没有一种标准的方法来非常有效地做到这一点?(我无法测试所有可能的配置,我需要标准或良好的体验感受响应).
注1:我不想使用asm的解决方案
注2:出于兼容性原因,我不使用C++ 11
template<typename T>
struct raster {
std::vector<T> v;
template<typename U, typename = std::enable_if_t<sizeof(T) == sizeof(U)>>
raster(raster<U>&& other){
// What code goes here?
}
}
Run Code Online (Sandbox Code Playgroud)
假设我们有raster<uint32_t> r这样r.v.size()以百万计,并保证它的所有元素都内int32_t的范围.是否raster<int32_t>::raster(raster<uint32_t>&& other)可以避免复制内存支持other.v?
或者我应该做些什么*reinterpret_cast<raster<int32_t>*>(&r)而不是调用那个构造函数?
我上课了.但是当我为它添加静态向量和getter时,我遇到了编译错误.
这是一个例子:
// Config.h file
class Config {
static std::vector<std::string> m_RemoteVideoUrls;
...
public:
static std::vector<std::string> GetRemoteVideoURLs();
};
// Config.cpp file
static std::vector<std::string> m_RemoteVideoUrls = {"some url"};
...
std::vector<std::string> Config::GetRemoteVideoURLs() {
return m_RemoteVideoUrls;
}
Run Code Online (Sandbox Code Playgroud)
我在Visual Studio 2017中编译期间遇到了这个奇怪的错误
1>config.obj : error LNK2001: unresolved external symbol "private: static class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > Config::m_RemoteVideoUrls" (?m_RemoteVideoUrls@Config@@0V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@A)
Run Code Online (Sandbox Code Playgroud)
经过几次实验,我明白了我的错误m_RemoteVideoUrls.因为这个存根工作:
std::vector<std::string> Config::GetRemoteVideoURLs() {
return std::vector<std::string>{"a", "b"};// m_RemoteVideoUrls;
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
std::vector<std::string> Config::GetRemoteVideoURLs() {
LoadConfigIfRequired();
std::vector<std::string> tmp = m_RemoteVideoUrls; …Run Code Online (Sandbox Code Playgroud) Snippet1:以下代码段打印出0 1但返回一个空向量.
vector<int> trial() {
vector<int> ret;
ret.reserve(2);
ret[0] = 0;
ret[1] = 1;
cout << ret[0] << " " << ret[1] << "\n";
return ret;
}
Run Code Online (Sandbox Code Playgroud)
代码段2:以下代码段打印出0 1并返回包含{0,1}的向量.
vector<int> trial() {
vector<int> ret;
ret.push_back(0);
ret.push_back(1);
cout << ret[0] << " " << ret[1] << "\n";
return ret;
}
Run Code Online (Sandbox Code Playgroud)
为什么代码片段1不像代码片段2那样工作.如果我保留内存并向向量添加值.
我正在处理无法更改数据类型的库。我需要调用一个需要bool数组的函数。我正在使用的代码std::vector<bool>用于存储布尔值。我已经阅读了很多有关std::vector<bool>SO的问题以及与之相关的问题,但是我还没有找到针对我的问题的最优雅的解决方案。这是性能至关重要的代码。如何以最有效的方式处理此问题?我可以更改存储在中的类型std::vector,但不能逃避使用std::vector。
对于我自己的问题,我必须调用Fortran函数,但是我在C ++中做了一个最小的例子来说明问题。
#include <cstdio>
#include <vector>
void print_c_bool(bool* bool_array, size_t n)
{
for (int i=0; i<n; ++i)
printf("%d, %d\n", i, bool_array[i]);
}
int main()
{
// This works.
bool bool_array[5] = {true, false, false, true, false};
print_c_bool(bool_array, 5);
// This is impossible, but how to solve it?
std::vector<bool> bool_vector {true, false, false, true, false};
// print_c_bool(bool_vector.data(), bool_vector.size());
return 0;
}
Run Code Online (Sandbox Code Playgroud)