标签: std

给定一个字符串,如何只向它添加另一个字符串中的唯一字符?

我几个小时以来一直在尝试许多不同的东西,我尝试使用std::unique但我得到了时髦的结果,然后我尝试使用string::find。我觉得如果我使用std::vector会很容易做到这一点 我正在寻找一种使用标准库的有效方法来做到这一点,我阅读了很多关于这里和 cpluplus.com 的问题但我无法使用他们的答案来实现我所需要的。如果这是微不足道的,我深表歉意,但此时我已经厌倦了尝试不同的事情。

例如:

int main(){

  std::string unique_chars;
  std::string new_string = "ABC"

  getUniqueChars(unique_chars, new_string);
  cout << unique_chars << endl;

  new_string = "ABDF"
  getUniqueChars(unique_chars, new_string);

  cout << unique_chars; 

  return 0;
}  

void getUniqueChars(string &unique_chars, string &new_string){

   //add only unique characters from new_string to unique_chars

   return;
}
Run Code Online (Sandbox Code Playgroud)

应该输出:

ABC
ABCDF
Run Code Online (Sandbox Code Playgroud)

c++ unique std string-formatting c++11

0
推荐指数
1
解决办法
1130
查看次数

具有相同参数的 C++ std::sort

我正在阅读别人的代码,我看到如下内容:

sort(myvec.begin(),  myvec.begin());
Run Code Online (Sandbox Code Playgroud)

我写了一些代码来测试它,它似乎和

sort(myvec.begin(),  myvec.end());
Run Code Online (Sandbox Code Playgroud)

他们真的做同样的事情吗?有记录吗?有没有办法理解为什么,或者以这种方式实现它只是一个随意的选择?

回答后更新

是的,这是一个无操作。我的测试代码有一个错误。

c++ sorting std

0
推荐指数
1
解决办法
63
查看次数

从列表创建 C++ 离散分布

我正在尝试创建一个 std::discrete_distribution:

http://en.cppreference.com/w/cpp/numeric/random/discrete_distribution

我看到的所有例子都像上面的链接一样定义它 std::discrete_distribution<> d({40, 10, 10, 40});

但是{40, 10, 10, 40}我不想以编程方式而不是硬编码来执行此操作。

c++ std

0
推荐指数
1
解决办法
1311
查看次数

删除 std::map 的函数

我正在尝试删除以下地图:

typedef bool (myClass::*func)(std::vector<std::string> &args);
typedef std::map<std::string, func> myMap;
myMap map; //map of functions in the class 'myClass'

void deleteMap()
{
    for(myMap::iterator it = map.begin(); it != map.end(); ++it)
    {
        delete it->second; //compiler says it->second is non-pointer type
        map.erase(it);
    }
}
Run Code Online (Sandbox Code Playgroud)

'map' 将字符串映射到类 'myClass' 中的函数,该函数在其参数中采用字符串向量。

在我尝试删除此映射时,我试图删除指向成员函数的指针,然后擦除迭代器本身。编译器说 it->second 必须是指针类型。在 typdef 'func' 是一个指向 myClass:: 的指针,那么为什么我会收到这个错误?

这是删除函数映射的合适方法吗?

c++ pointers stl stdmap std

0
推荐指数
1
解决办法
1112
查看次数

使用迭代器从 std::string 中获取子字符串

我想从字符串中 char 的第一次出现到字符串末尾获取一个子字符串。我以为我可以只使用构造函数,就像在这个问题中一样,但它并没有真正起作用。当我这样做时:

string(input.find(' ')+1, input.end()-1)
Run Code Online (Sandbox Code Playgroud)

我面临“无构造函数”错误

error: no matching constructor for initialization of 'std::__cxx11::string' (aka 'basic_string<char>')
Run Code Online (Sandbox Code Playgroud)

如何解决此问题并使我的代码正常工作?

c++ string iterator std

0
推荐指数
1
解决办法
4454
查看次数

如何在 C++ 向量中解压多个值

我有这个函数可以从这里为我返回 RGB 格式的颜色

std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
Run Code Online (Sandbox Code Playgroud)

但现在我还希望函数find_dominant_colors生成的图像返回,以便我可以使用它。它生成我使用的三个图像,cv::imwrite但我希望将这三个图像返回给函数调用,以便在返回时可以直接进一步查看它,而不是为它获取目录。

如何在该行代码中解压缩多个值,例如获取图像和颜色,而不仅仅是颜色。我必须使用多个向量吗?我怎么做 ?这里使用的向量是一个 opencv 向量,用于从图像中获取 RGB 值。

编辑 :

    std::vector<cv::Vec3b> find_dominant_colors(cv::Mat img, int count) {
    const int width = img.cols;
    const int height = img.rows;
    std::vector<cv::Vec3b> colors = get_dominant_colors(root);

    cv::Mat quantized = get_quantized_image(classes, root);
    cv::Mat viewable = get_viewable_image(classes);
    cv::Mat dom = get_dominant_palette(colors);

    cv::imwrite("./classification.png", viewable);
    cv::imwrite("./quantized.png", quantized);
    cv::imwrite("./palette.png", dom);

    return colors;
}


Run Code Online (Sandbox Code Playgroud)

上面的函数将颜色返回到这里

std::vector<cv::Vec3b> colors = find_dominant_colors(matImage, count);
Run Code Online (Sandbox Code Playgroud)

我也希望它返回viewable quantized dom,我该怎么做?

c++ opencv vector std stdvector

0
推荐指数
1
解决办法
253
查看次数

当键值是 std 向量时,为什么在 C++ 中使用 at 访问映射值如此之慢?

我正在使用std::map定义为std::map<std::vector<int>, double>,您会看到键值是整数向量。我的地图中的成员数量是 24600。这是最小的工作示例:

InOutLetFileVelocityWeights.h

#include <iostream>
#include <string>
#include <vector>
#include <map>

class InOutLetFileVelocityWeights
{
        public:
          InOutLetFileVelocityWeights();

          const std::string& GetWeightsFilePath()
          {
            return velocityWeightsFilePath;
          }
          void SetWeightsFilePath(const std::string& path)
          {
            velocityWeightsFilePath = path;
          }

          double GetValue(std::vector<int>& xyz);

          void Initialise();

        private:
          std::string velocityWeightsFilePath;

          std::map<std::vector<int>, double> weights_table;
};
Run Code Online (Sandbox Code Playgroud)

InOutLetFileVelocityWeights.cc

#include "InOutLetFileVelocityWeights.h"
#include <algorithm>
#include <fstream>
#include <cmath>

InOutLetFileVelocityWeights::InOutLetFileVelocityWeights()
{
}

double InOutLetFileVelocityWeights::GetValue(std::vector<int>& xyz)
{

      double value;

      value = weights_table.at(xyz);

      return value;

}

void InOutLetFileVelocityWeights::Initialise()
{
/* Load …
Run Code Online (Sandbox Code Playgroud)

c++ containers stl std c++11

0
推荐指数
1
解决办法
299
查看次数

将 int 数组转换为 std::vector&lt;int*&gt;

我正在尝试将整数 C 样式数组“转换”为std::vector<int*>不使用for循环,或者更确切地说是在向量中插入所有数组项的指针。

我目前正在通过执行以下操作来实现这一点:

for (size_t i = 0; i < 5; i++)
{
    vector.push_back(&values[i]);
}
Run Code Online (Sandbox Code Playgroud)

是否可以通过使用std::vector::insert()and来实现std::being/end

c++ std

0
推荐指数
1
解决办法
256
查看次数

为什么“应使用 'empty' 方法来检查是否为空,而不是使用 clang-tidy 的 'size”?

执行检查时:

std::string st = "hello";
bool is_empty = st.size() > 0;
Run Code Online (Sandbox Code Playgroud)

我收到了上面提到的 Clang-Tidy 警告。

为什么最好使用该empty()方法?

c++ containers std stdvector clang-tidy

0
推荐指数
1
解决办法
334
查看次数

将 std::array 变量作为参数传递

我需要帮助来解决我面临的问题。

我想要一个函数原型,它在两个实例中接受两种不同类型的参数。

例如如下所示: 我有一个如下所示的结构:

         typedef struct BufferStruct
         {
            unsigned long    BufferType;
            unsigned long    TheAddress;
          } Buffer;
Run Code Online (Sandbox Code Playgroud)

我有一个名为 Test 的函数,这个函数应该接受两种不同类型的参数

  1) std::array<Buffer, 2> axBuffer; 
  2) Buffer axBuffer;

   void Test(??)/* function argument ?*/

 These are C functions and not C++.

 Can someone please help me in getting the appropriate function prototype for the function named "Test"?

 Advanced thanks.
Run Code Online (Sandbox Code Playgroud)

c c++ std

0
推荐指数
1
解决办法
59
查看次数