小编son*_*yao的帖子

调整矢量矢量的大小

下面是用于调整矢量矢量大小的代码块.为每行大小生成的输出打印为0?即使我将每一行调整为W,为什么会发生这种情况;

int main() {
    int H,W,i;
    cin >> H,W; // H=3,W=5;
    vector<vector<int> >v;
    v.resize(H);

    for(i=0;i<H;i++)
        v[i].resize(W);

    cout << v[1].size(); // Output is printed 0 
}
Run Code Online (Sandbox Code Playgroud)

c++ vector operator-precedence comma-operator

-1
推荐指数
1
解决办法
276
查看次数

CppUnit:为什么静态局部变量保持其值?

我正在尝试使用CppUnit来测试一个只在第一次调用时才执行某些代码的方法.

class CElementParseInputTests: public CppUnit::TestFixture {
private:
    CElement* element;
public:

    void setUp() {
        element = new CElement();
    }

    void tearDown() {
        delete element;
    }

    void test1() {
        unsigned int parsePosition = 0;
        CPPUNIT_ASSERT_EQUAL(false, element->parseInput("fäil", parsePosition));
    }

    void test2() {
        unsigned int parsePosition = 0;
        CPPUNIT_ASSERT_EQUAL(false, element->parseInput("pass", parsePosition));
    }
Run Code Online (Sandbox Code Playgroud)

我想测试的递归方法:

bool CElement::parseInput(const std::string& input, unsigned int& parsePosition) {
    static bool checkedForNonASCII = false;
    if(!checkedForNonASCII) {
        std::cout << "this should be printed once for every test case" << std::endl;
        [...]
        checkedForNonASCII = …
Run Code Online (Sandbox Code Playgroud)

c++ oop static cppunit initialization

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

如何初始化vector <char>的向量?

我有来自数独拼图的数据.我必须在下面定义一些内容vector<vector<char>>.

[[".", "8", "7", "6", "5", "4", "3", "2", "1"], 
 ["2", ".", ".", ".", ".", ".", ".", ".", "."],
 ["3", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["4", ".", ".", ".", ".", ".", ".", ".", "."],
 ["5", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["6", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["7", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["8", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["9", ".", ".", ".", ".", …
Run Code Online (Sandbox Code Playgroud)

c++ initialization vector

-1
推荐指数
1
解决办法
444
查看次数

我从int x得到了一个非常奇怪的输出[3];

所以我在C++中键入下面的代码

#include <iostream>
using namespace std;
int main() {
    int x[3];
    cout << x[1] << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,它打印出-272632344而不是0.任何理由?

c++ initialization

-1
推荐指数
1
解决办法
64
查看次数

flush和endl有什么区别?

我意识到输出是一样的.
情况1:

cout << "enter password " <<flush;      
Run Code Online (Sandbox Code Playgroud)

案例2:

cout << "enter password " <<endl;  
Run Code Online (Sandbox Code Playgroud)

c++

-2
推荐指数
1
解决办法
1055
查看次数

C++中2-D std :: array的Braced初始化列表

如何用std::array?初始化数组?我的解决方案是

array<array<int, sze>, sze> arcpp { 0, 1, 2, 3 , 4, 5, 6, 7, 8, 9, 10, 11 ,12, 13, 14, 15 };
for (auto i = 0; i < sze; i++) {
    cout << "\n";
    for (auto j = 0; j < sze; j++) {
        cout << "\t" << arcpp[i][j];
    }
}
Run Code Online (Sandbox Code Playgroud)

但我想要这样的事情:

int matrix[][sze] = {
    {0, 1, 2, 3},
    {4, 5, 6, 7},
    {8, 9, 10, 11},
    {12, 13, 14, 15}
};
Run Code Online (Sandbox Code Playgroud)

c++ arrays initialization matrix c++11

-2
推荐指数
1
解决办法
60
查看次数

模板化代理设计模式

我有一个简单且可重现的代码,看起来像这样:

template <typename T>
class Proxy {
private:
    Wrap<T> &self; // If I comment this, it will work
public:
    Proxy() {}
};

template <typename T>
class Wrap {
    T *p;
public:
    Proxy<T> foo() {
        return Proxy<T>();
    }
};

int main()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

'Wrap'没有命名类型

如果我评论Wrap<T> &self,那么它会起作用,但这不是我需要的.我需要Wrap<T>成为Proxy班上的一员.我怎样才能做到这一点?

c++ templates declaration forward-declaration

-2
推荐指数
1
解决办法
88
查看次数

范围为for循环中的多个语句

我想知道是否可以转换这个表达式

vector<Mesh>::iterator vIter;
for(int count = 0, vIter = meshList.begin(); vIter < meshList.end(); vIter++, count++)
{
...
}
Run Code Online (Sandbox Code Playgroud)

进入类似于C++ 11的东西

我想得到这样的东西:

for(auto count = 0, auto mesh : meshList; ; count++)
{ 
... 
}
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

c++

-2
推荐指数
1
解决办法
74
查看次数

当基类在 C++ 中具有交换函数时,为什么名称查找找不到 std::swap?

我有我代码中内容的副本

#include <utility>
using namespace std;

class Parent
{
    public:
        void swap(Parent*);
};
class A : public Parent 
{
    public:
        void handle();
};


void A::handle()
{

    long x = 1;
    long y = 2;
    swap(x,y);
}


int main()
{
    A v;
    v.handle();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误日志:

main.cpp: In member function 'void A::handle()':
main.cpp:21:10: error: no matching function for call to 'A::swap(long int&, long int&)'
  swap(x,y);
          ^
main.cpp:7:8: note: candidate: void Parent::swap(Parent*)
   void swap(Parent*);
        ^
main.cpp:7:8: note:   candidate expects 1 argument, …
Run Code Online (Sandbox Code Playgroud)

c++ name-lookup argument-dependent-lookup c++11

-2
推荐指数
1
解决办法
90
查看次数

如何让push_back在C++程序中工作

我正在进行C++任务,并且在push_back方面存在一些错误问题.错误消息显示:

没有匹配的成员函数来调用'push_back'.

该错误发生在以下行: book.push_back(name,number,email);

这是代码:

//Program

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

// Declaring ye ol' phonebook structure //
struct phoneBook {
public:
    string name;
    int number;
    string email;
};

int main(){
    // Declaring the VECTOR of the Phonebook
    vector<phoneBook>book;
    string fileName;
    //reading the file
    cout <<"Enter file name to read contacts: ";
    cin >> fileName;

    std::string myline;
    std::ifstream infile(fileName);
        while (std::getline(infile, myline))
    {
        std::istringstream iss(myline);
        string name;
        int number;
        string email;
        if(!(iss …
Run Code Online (Sandbox Code Playgroud)

c++ vector push-back

-3
推荐指数
1
解决办法
823
查看次数

为什么strcat导致分段错误

任何人都可以向我解释为什么以下代码导致分段错误?buff应足够长,可容纳128个字符.

int main () {
    char buff[16384];
    char buff2[128];
    sprintf(buff2, "MinPer(PatternName_Equal27_xxxxxxx_MasterPatSetup.PatternName_Equal27_xxxxxxx__default_WFT___WFTRef.ActualT0Period.UserPeriod_2_1)" );
    strcat(buff, buff2);
    std::cout << buff2 << endl;
    std::cout << buff << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ segmentation-fault

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

在C中添加字段值?

我想问你如何在C中添加数组的值.示例:

Array[5]
--------
Array[1]=0.2
Array[2]=0.2 ----> I want to add the first 0.2 to the second 0.2 (=0.4)
Array[3]=0.3
Array[4]=0.15
Run Code Online (Sandbox Code Playgroud)

我想要以下输出:

0.2
0.4
0.7
0.85
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?特约经营者?PS:我想在一个简单的for循环中做到这一点.不是额外的图书馆等

c arrays

-9
推荐指数
1
解决办法
97
查看次数