我需要从std::vector.
我知道这是可以做到的,myvector.erase(myvector.begin() + index)但这对我来说看起来很难看。
像这样的东西myvector.removeindex(index)会更具可读性。
他们是否在 C++20 或 C++23 中计划类似的事情?
我是c ++的新手,但是有半年的Java se/ee7工作经验.
我想知道如何将3个值放入vector<string>
示例中:vector<string, string, string>或者只是vector<string, string>
为了避免使用3个向量.
vector<string> questions;
vector<string> answers;
vector<string> right_answer;
questions.push_back("Who is the manufacturer of Mustang?");
answers.push_back("1. Porche\n2. Ford \n3. Toyota");
right_answer.push_back("2");
questions.push_back("Who is the manufacturer of Corvette?");
answers.push_back("1. Porche\n2. Ford \n3. Toyota \n4. Chevrolette");
right_answer.push_back("4");
for (int i = 0; i < questions.size(); i++) {
println(questions[i]);
println(answers[i]);
if (readInput() == right_answer[i]) {
println("Good Answer.");
} else {
println("You lost. Do you want to retry? y/n");
if(readInput() == "n"){
break;
}else{ …Run Code Online (Sandbox Code Playgroud) 在下面的代码,push_back()一std::ref成std::vector<reference_wrapper<Type>>行之有效然而,分配std::ref到reference_wrapper<Type>不起作用.为什么?
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
struct Type {};
int main()
{
Type t1;
vector<reference_wrapper<Type>> t2;
t2.push_back( ref(t1) ); // OK
//reference_wrapper<Type> t3; // error: no matching function for call to std::reference_wrapper<Type>::reference_wrapper()’
//t3 = ref(t1);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个执行类的下面程序,该程序填充下面所示的映射
map<string,map<string,vector<StructAbsTypeObject>>>
Run Code Online (Sandbox Code Playgroud)
在这里,我正在创建共享对象并分配它们,它们在第一次检查时有效,但是在第二次检查时,shared_ptr返回null。我需要知道原因。该代码看起来不错,但不知道哪里出了问题。
//Code begins
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <memory>
using namespace std;
class Test {
public:
Test(int i):t(i) {
}
private:
int t;
};
class ConcTypeObject {
public:
ConcTypeObject() {
}
ConcTypeObject(const ConcTypeObject& other) {
m_ptr_Test = other.m_ptr_Test;
}
ConcTypeObject& operator=(const ConcTypeObject& other) {
m_ptr_Test = other.m_ptr_Test;
}
void setTest(shared_ptr<Test> ptr) {
cout << "setTest" << endl;
m_ptr_Test = ptr;
}
shared_ptr<Test> getTest() {
return m_ptr_Test;
}
bool isValid() {
if(m_ptr_Test) {
return true; …Run Code Online (Sandbox Code Playgroud) 我一直在工作,vector < pair < int,int>>但我不知道类似的东西vector< int,int>也存在,我可以声明它,但不知道如何使用它。
是vector < int,int>一个有效的容器,如果是,那和之间的区别是什么vector < pair < int,int>>?
如果没有,我为什么可以宣布呢?
我有一个向量L,其中每个向量包含2个双打。我想按它们的第一个元素对L中的向量进行排序。
vector<vector<double>> L;
Run Code Online (Sandbox Code Playgroud)
我尝试将std :: sort算法与自写的compare函数结合使用,如下所示:
bool compare(const vector<double> &v1, const vector<double> &v2)
{
return v1[0] < v2[0];
}
void out_2d_vecotr_as_value_pairs(vector<vector<double>> &L)
{
L = sort(L.begin(), L.end(), compare);
...
}
Run Code Online (Sandbox Code Playgroud)
但是对于调用sort()函数的行,我得到了我不理解的错误:
projet.cc:234:38: error: no match for ‘operator=’ (operand types are ‘std::vector<std::vector<double> >’ and ‘void’)
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
我需要使用算法库中的函数查找向量元素的索引。
\n例子:
\n{1,2,3,4,5,6,7,8,9,10}
\n元素 5 在 5 位置找到。
\n#include <algorithm>\n#include <iostream>\n#include <vector>\nbool comp(int a, int b) { return a < b; }\nint main() {\n int n = 10;\n std::vector<int> a{10, 8, 5, 4, 1, 2, 3, 6, 7, 9};\n sort(a.begin(), a.begin() + n, comp);\n int number = 5;\n std::vector<int>::iterator it;\n it = std::search_n(a.begin(), a.begin() + n, number);\n if (it != a.end())\n std::cout << "found at position " << (it - a.begin()) << '\\n';\n else\n std::cout << …Run Code Online (Sandbox Code Playgroud) vector<bool>我正在尝试在 C++ 中为 50,000,000,000 个条目分配 a ;但是,程序出错了。在抛出 'std::bad_alloc' What(): std::bad_alloc 实例后调用终止(或者在在线编译器中它刚刚结束)。
我最初以为这是由于尺寸太大造成的;然而,v1.maxsize()对我来说已经超过50GB了。但令人困惑的是,当我减少条目数量时,它工作得很好。
问题:考虑到条目数小于向量的最大大小,根本原因可能是什么?
其他问题/答案表明类似的问题是由于在 32 位 cpu 上造成的;不过我有一个64位的。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
long size = 50000000000;
std::vector<bool> v1;
std::cout << "max_size: " << bool(v1.max_size() > 50000000000) <<"vs" << size << "\n";
v1 = std::vector<bool>(size,false);
cout << "vector initialised \n" << endl;
cout << v1.size() << endl;
}
Run Code Online (Sandbox Code Playgroud)
注意:我本质上是试图创建一个内存高效位图来跟踪不同数据结构的某些地址是否已初始化。我无法使用本文中提到的位集,因为其大小在编译时未知。
=========================================
我在某处读到,只有索引访问运算符[]和()成员函数之间的区别在于()也检查索引是否有效.但是,从以下代码中扣除,似乎存在差异
std::vector<std::string>* namesList = readNamesFile("namesList.txt");
std::vector<Rabbit> rabbits;
const int numNAMES = namesList->size();
for (int i = 0; i < 5; i++)
{
rnd = rand() % numNAMES;
rabbits.push_back(Rabbit(namesList[i]));
}
Run Code Online (Sandbox Code Playgroud)
上面的代码抛出
error C2440: '<function-style-cast>' : cannot convert from 'std::vector<std::string,std::allocator<_Ty>>' to 'Rabbit'
1> with
1> [
1> _Ty=std::string
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
Run Code Online (Sandbox Code Playgroud)
此外,如果我将鼠标悬停在上面(见下文)
rabbits.push_back(Rabbit(namesList[i]));
Run Code Online (Sandbox Code Playgroud)
^^^^^^^我读了intelliSense:
Error: no instance of constructor …Run Code Online (Sandbox Code Playgroud) 我编写了以下代码,以测量1000k不使用保留和保留的情况下推回整数时间所花费的时间。结果不是我想要的。
所有测试均在我的Samsung ativtap7上执行,该处理器具有在Windows 10下运行的核心i5 @ 1.8 Ghz处理器,4 GB RAM和VS2018 C ++编译器。
#include <iostream>
#include <vector>
#include "Stopwatch.h"
using namespace std;
int main()
{
Stopwatch myWatch;
//pushback 1000k times without reserve
for (int i = 0; i < 10; i++)
{
cout << "try " << i + 1 << endl;
myWatch.Start();
vector<int> vec1;
for (int i = 0; i < 1000000; i++)
{
vec1.push_back(i);
}
myWatch.End();
myWatch.LookElapsedTime();
//pushback 1000k times with reserve
myWatch.Start();
vector<int> vec2(1000000);
for (int i …Run Code Online (Sandbox Code Playgroud)