小编JeJ*_*eJo的帖子

按字母顺序对结构数组进行排序

我有一个名为Sheep的结构数组(A [#])(因为我的任务是关于绵羊DNR).在我做任何要求的任务后,我留下了这个结构:

struct Sheep 
{
    string Vardas;
    char Fragmentas[CMax]; 
    int atitikme = 0; 
};
Run Code Online (Sandbox Code Playgroud)

在我的内部,数据是:

(string Vardas) | (char Fragmentas[CMax]) | (int atitikme)
Baltukas   TAGCTT 3
Bailioji   ATGCAA 3 
Smarkuolis AATGAA 1 
Run Code Online (Sandbox Code Playgroud)

(char Fragmentas [CMax]将不会使用,所以你不必看它,我只是将它命名为明确).

所有这些数据都来自U2.txt文件,无法在代码中手动输入.

它剩下要做的就是按照这些规则对它进行排序:

  1. 它通过'int atitikme'从大到小.
  2. 如果'int atitikme'相等,那么它必须按'A [#]排序.Vardas按字母顺序排列.

要通过'int atitikme'对其进行排序,我创建了一个代码:

string q;
char w[20];
int e;
for (int o = 0; o < n-1; o++) 
{
    for (int p = o+1; p < n-1; p++) 
    {
        if (A[p].atitikme > A[o].atitikme) 
        {
            // - Vardo Keitimas
            q …
Run Code Online (Sandbox Code Playgroud)

c++ sorting algorithm stdvector c++11

4
推荐指数
1
解决办法
337
查看次数

sizeof运算符可以在C++中运行什么操作数?sizeof(函数)有意义吗?

我有点困惑sizeof(function).我以前认为sizeof运算符对类对象,指针和引用进行操作.

sizeof运算符可以在C++中运行什么操作数

是否sizeof有意义?

c++ function sizeof language-lawyer c++11

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

通过emplace()将对象指针插入到地图的地图中不起作用

我正在尝试将指针对象插入到map通过emplace()但它不起作用.

我在下面创建了一个简单的问题表示.我正在尝试插入newFooList指针对象类型Foo*.

我似乎找不到为FooMap*in 创建类型的方法std::map<int, FooMap*> m_fooMapList.是否应该使用new地图的第二个字段?

#include <iostream>
#include <utility>
#include <stdint.h>
#include <cstdlib>
#include <map>

class Foo
{
    private:
        int m_foobar;
    public:
        Foo(int value)
        {
            m_foobar = value;
        }
        void setfoobar(int value);
        int getfoobar();
};

class FooMap
{
    private:
        std::map<int, Foo*> m_newFoo;

    public:
        FooMap() = default;
};

class FooMapList
{
    private:
        std::map<int, FooMap*> m_fooMapList;
    public:
        FooMapList() = default;
        void insertFoo(Foo* newFooObj);
};

int Foo::getfoobar(void)
{ …
Run Code Online (Sandbox Code Playgroud)

c++ pointers stdmap c++11 emplace

4
推荐指数
1
解决办法
505
查看次数

如何在不重载 `operator()`、`std::less`、`std::greater` 的情况下为 `std::multiset` 提供自定义比较器?

我想要一个用于以下代码的自定义比较器。但是,我不允许重载 operator(), std::less, std::greater

我试图使用 lambda 来实现这一点,但gcc不允许我auto用作非静态成员。还有其他方法可以使这项工作吗?

#include <iostream>
#include <map>
#include <set>

class Test 
{
public:
    // bool operator () (const int lhs, const int rhs) { // not allowed
    //     return lhs > rhs;
    // };    
    using list = std::multiset<int  /*, Test*/>;
    std::map<const char*, list> scripts;
};

int main() 
{
    Test t;
    t.scripts["Linux"].insert(5);
    t.scripts["Linux"].insert(8);
    t.scripts["Linux"].insert(0);

    for (auto a : t.scripts["Linux"]) {
        std::cout << a << std::endl;
    }

    std::cout << "end";
} …
Run Code Online (Sandbox Code Playgroud)

c++ lambda multimap custom-compare c++11

4
推荐指数
1
解决办法
1071
查看次数

如何调用操作员模板?

我对如何实例化此模板感到有些困惑。我知道,简单地使用friend会员身份会实现我想要的东西会更容易,但是如果我强迫这样做,该怎么办?我只是想弄清楚。(顺便说一句,我知道这个模板似乎毫无意义),我只想使其编译即可。

#include <iostream>

template <typename T>
inline std::ostream& operator<< (std::ostream& os, const T& date)
{
    os << date.getD() << " " << date.getM() << " " << date.getY() << "\n";
    return os;
}

class Date 
{
private:
    int dd, mm, yy;
public:
    Date(int d, int m, int y) : dd(d), mm(m), yy(y) {}
    int getD() const;
    int getM() const;
    int getY() const;
};

int Date::getD() const {  return dd; }

int Date::getM() const {  return mm; }

int …
Run Code Online (Sandbox Code Playgroud)

c++ templates class operator-overloading c++11

4
推荐指数
1
解决办法
103
查看次数

4
推荐指数
1
解决办法
76
查看次数

一对数组而不是 STL 映射 - 可能吗?优缺点都有什么?

为什么 c++ 标准映射非常有名,而成对数组也与那些相似?

什么时候使用 C++ 标准映射更好,什么时候使用成对数组?或者两者的应用程序相似?

c++ arrays stdmap c++-standard-library data-structures

4
推荐指数
1
解决办法
382
查看次数

如何将参数列表存储到向量?

如何将可变参数构造函数参数存储到向量?

我失败的尝试示例:

class Combo 
{
public:
   template <class... Args>
   Combo(Args... args) 
   {
      // this->keys_.push_back(args...);

      // this->keys_.push_back(args)...;

      // this->keys_.push_back(std::forward<Args>(args...));

      //for (uint8_t arg : args...)
      //  this->keys_.push_back(arg);

      // ???
   }

private:
   std::vector<uint8_t> keys_;
};
Run Code Online (Sandbox Code Playgroud)

c++ templates constructor stdvector variadic-templates

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

关于容器的迭代器

我不知道,为什么它输出1024

vector<int> default_container = { 1,2,3,4,5,6,7,78,8,1024 };
cout << *default_container.end() << endl; // 0
default_container.pop_back();

for (auto it : default_container) 
{
    cout << it << ",";
}
cout << endl;

cout << *default_container.end() << endl;   // 1024 why?why?why?why?
cout << *--default_container.end() << endl; // 8

Run Code Online (Sandbox Code Playgroud)

c++ iterator stdvector dereference c++11

4
推荐指数
1
解决办法
133
查看次数

模板参数作为函数未命名参数

这个问题继续 非静态数据成员类推导

这是未命名的参数函数,我用它来返回std::string数据类型的表示

struct Boo {};
struct Foo {};

std::string class2str(const double) { return "Floating"; };
std::string class2str(const int) { return "Fixed Point"; };
std::string class2str(const Foo) { return "Class Foo"; };
std::string class2str(const Boo) { return "Class Boo"; };

int main(int argc, char* argv[]) 
{
    int    x_a;
    double x_b;
    Foo    F;
    Boo    B;
    std::cout << "x_a     :" << class2str(x_a) << std::endl;
    std::cout << "x_b     :" << class2str(x_b) << std::endl;
    std::cout << "Foo     :" << class2str(F) << std::endl; …
Run Code Online (Sandbox Code Playgroud)

c++ templates class function-templates c++17

4
推荐指数
1
解决办法
128
查看次数