下面是用于调整矢量矢量大小的代码块.为每行大小生成的输出打印为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) 我正在尝试使用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) 我有来自数独拼图的数据.我必须在下面定义一些内容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++中键入下面的代码
#include <iostream>
using namespace std;
int main() {
int x[3];
cout << x[1] << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它打印出-272632344而不是0.任何理由?
我意识到输出是一样的.
情况1:
cout << "enter password " <<flush;
Run Code Online (Sandbox Code Playgroud)
案例2:
cout << "enter password " <<endl;
Run Code Online (Sandbox Code Playgroud) 如何用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) 我有一个简单且可重现的代码,看起来像这样:
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班上的一员.我怎样才能做到这一点?
我想知道是否可以转换这个表达式
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)
有没有办法做到这一点?
我有我代码中内容的副本
#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++任务,并且在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) 任何人都可以向我解释为什么以下代码导致分段错误?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中添加数组的值.示例:
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循环中做到这一点.不是额外的图书馆等