下面这篇文章,我在那里找到了一个临时的解决方法我的其他问题,我想知道我是否可以更换int argc, char** argv一个std::vector<std::string>变量/对象。
考虑假想的代码:
#include <iostream>
#include <CloseLibrary>
void someFunction(int argc, char** argv){
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
}
int myFunc(int argc, char** argv){
someFunction(argc, argv);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
其中CloseLibrary是一个封闭的库,我无法访问源代码,而该someFunction库中的函数需要int argc, char** argv命令行参数。但出于某种原因char**,我的代码中不能有双指针。
在这篇文章中,我提出了类似我需要的东西,但我不知道如何使用它。我可以这样写代码吗:
#include <iostream>
#include <CloseLibrary>
#include <vector>
void someFunction(int argc, char** argv){
for (int i = 0; …Run Code Online (Sandbox Code Playgroud) 我想在 vectorb的某个位置插入vector a。例如
std::vector <int> vecta{ 10, 20, 30 ,40 , 50};
std::vector <int> vectb{ 1000, 2000, 3000 };
Run Code Online (Sandbox Code Playgroud)
我怎么能让结果向量像 {10,20,1000,2000,3000,40,50} ?我想删除30它并用向量替换它。
我想以常数时间复杂度 O(1) 删除整数向量的最后 n 个元素。我认为这应该是可行的,因为只需要对结束指针进行一些算术运算。然而,擦除、调整大小和删除都具有 O(n) 复杂度。有什么功能可以使用?
如何正确且轻松地初始化class包含std::vector某个其他类的 a 的实例,该类本身包含一些数据。
我知道用文字来解释它真的很难,所以我会写一段不起作用的代码,但它抓住了我的意图。
#include <vector>
struct Point
{
float x, y;
};
struct Triangle
{
Point points[3];
};
struct Geometry
{
std::vector<Triangle> triangles;
};
int main()
{
Geometry instance
{
{{0,0}, {6, 0}, {3, 3}},
{{5,2}, {6, 6}, {7, 3}}
};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码不起作用。Clang 返回错误 -
excess elements in struct initializer
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它给我这个错误。
我想我可以初始化
std::vector的Triangle小号,Points的数组,float每个Point对象中的两个s 。我将如何Geometry使用一些值正确初始化类的实例而不使用初始化方括号编写太多代码?
如果您有其他选择,那么我愿意考虑它们。
我有一些类型的数据std::vector<uint8_t>。我想将它解释为一个字符串并对其进行一些子字符串检查。
可以通过将其转换为std::string这将导致复制数据来完成。是否有可能以某种方式将其转换为std::string_view并对其进行搜索以避免复制它。
我尝试使用以下线程中的解决方案在具有固定大小的类中创建向量向量,但无济于事。使用 boost 分配初始化具有固定大小的向量的向量
由于它已有 7 年历史,我认为这可能与 C++17 的变化有关,但我不确定问题出在哪里。IDE 告诉我的错误是第一个参数上的“预期类型说明符”。查看构造函数的文档,似乎没有什么问题,除非我错过了一些东西。
class SudokuSolver {
public:
SudokuSolver() {}
~SudokuSolver() {}
private:
std::vector<std::vector<int>> sudoku_field(9, std::vector<int>(9, 0));
};
Run Code Online (Sandbox Code Playgroud) // The following code works fine, throwing a std::out_of_range exception:
std::vector<double> vd{ 1.5 };
try {
int i{ -1 };
double d = vd.at(i); // exception is thrown
}
catch (std::out_of_range& re) {
std::cout << "Exception is " << re.what() << std::endl; // invalid vector subscript
}
Run Code Online (Sandbox Code Playgroud)
如果我使用无效索引访问 for 循环中的向量元素,std::exception尽管我使用.at(). 为什么std::out_of_range不抛出异常?
// in a for loop, this does not throw the exception!
std::vector<double> vd{ 1.5 };
try {
for (int i = -1; i …Run Code Online (Sandbox Code Playgroud) 简化为最小的可重现示例,我遇到问题的代码如下:
#include <iostream>
#include <vector>
using namespace std;
void fill_vector_of_vectors(vector<vector<char>>& data) // I gave up on trying to think up a proper name for this
{
data.clear();
data.resize(4);
for (vector<char> subarray : data)
{
subarray.resize(4, 'x');
}
}
int main()
{
vector<vector<char>> data;
fill_vector_of_vectors(data);
for (vector<char> subarray : data)
{
for (char must_be_x: subarray)
{
cout << must_be_x;
}
cout << "\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
预期输出是
xxxx
xxxx
xxxx
xxxx
Run Code Online (Sandbox Code Playgroud)
但是代码的实际输出是四个空行。使用调试器,我发现填充函数确实正确填充了子向量,但子向量中的子向量main()是空的。我在哪里丢失数据?
前几天我解决了另一个问题,涉及一个std::vector <std::pair<int,int>>被调用的name.
我的问题是,如何访问这种类型的name.first和name.second?
我最终使用了一个 ranged-for 循环,这解决了我的问题
for(i : name) { i->first , i->second}
Run Code Online (Sandbox Code Playgroud)
但是,还有别的办法吗?我对如何在正常for循环中访问它特别感兴趣,例如
for(int i = 0; i < name.size(); i++) { std::vector::std::pair::name.first}
Run Code Online (Sandbox Code Playgroud)
有人可以为我解释一下吗?
这是我在 StackOverflow 上的第一个问题,所以如果我的问题中遗漏了任何内容或我没有遵循的某些规则,我会提前道歉。请编辑我的问题或在评论中告诉我我应该如何改进我的问题,谢谢。
我正在尝试std::vector<unsigned char>使用std::ifstream. 问题是文件似乎已成功读取,但向量仍为空。
这是我用于读取文件的函数:
void readFile(const std::string &fileName, std::vector<unsigned char> &fileContent)
{
std::ifstream in(fileName, std::ifstream::binary);
// reserve capacity
in.seekg(0, std::ios::end);
fileContent.reserve(in.tellg());
in.clear();
in.seekg(0, std::ios::beg);
// read into vector
in.read(reinterpret_cast<char *>(fileContent.data()), fileContent.capacity());
if(in)
std::cout << "all content read successfully: " << in.gcount() << std::endl;
else
std::cout << "error: only " << in.gcount() << " could be read" << std::endl;
in.close();
}
Run Code Online (Sandbox Code Playgroud)
这就是我调用函数的方式main():
std::vector<unsigned char> buf;
readFile("file.dat", buf);
std::cout << "buf size: " …Run Code Online (Sandbox Code Playgroud)