我听说Modern C++的一个建议是使用emplace_back而不是push_back在容器中追加(emplace_back接受容器中任何类型存储的构造函数的任何版本的参数).
根据标准草案N3797 23.3.6.5(1),说:
备注:如果新大小大于旧容量,则会导致重新分配.如果没有重新分配,插入点之前的所有迭代器和引用仍然有效.如果除了复制构造函数之外抛出异常,移动构造函数,赋值运算符或T的移动赋值运算符,或者通过任何InputIterator操作都没有效果.如果非CopyInsertable T的移动构造函数抛出异常,则不指定效果.
这指定了在不需要重新分配时会发生什么,但在容器需要增长时保持打开问题.
在这段代码中:
#include <iostream>
#include <vector>
int main() {
std::vector<unsigned char> buff {1, 2, 3, 4};
buff.emplace_back(buff[0]);
buff.push_back(buff[1]);
for (const auto& c : buff) {
std::cout << std::hex << static_cast<long>(c) << ", ";
}
std::cout << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译VC++(Visual Studio的2013更新4)和GCC 4.9.1(MinGW的)在调试在Windows 8.1.
使用VC++编译时,输出为:
1, 2, 3, 4, dd, 2
Run Code Online (Sandbox Code Playgroud)
使用GCC编译时,输出为:
1, 2, 3, 4, 1, …Run Code Online (Sandbox Code Playgroud) 好的,所以我一直收到这个错误:
$ gcc -Wall -g translate.c support.c scanner.c -o translate
translate.c: In function ‘main’:
translate.c:22:16: warning: assignment from incompatible pointer type [enabled by default]
dictionary = createArray(count);
^
support.c: In function ‘readTokens’:
support.c:66:18: warning: assignment from incompatible pointer type [enabled by default]
a[count] = token;
^
Run Code Online (Sandbox Code Playgroud)
而且我不知道为什么.
这是我的主要功能:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "support.h"
int main(int argc, char** argv) {
int i;
int count;
char** dictionary;
if (argc != 3) {
printf("need two arguments!\n");
exit(-1);
}
count = …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,它接受两个不同矩阵的元素,然后它将它们相乘,接下来它将它们保存在一个多维数组中.但它只适用于方阵.(Visual Studio 2013没有错误.)如果您帮助我编辑此代码以便将各种矩阵相乘,我将很高兴.
int a, b, c, d;
int Mat1[10][10];
int Mat2[10][10];
int Mat3[10][10];
Back1:
cout << endl << "Enter the number of rows in Matrix 1 : ";
cin >> a;
cout << endl << "Enter the number of columns in Matrix 1 : ";
cin >> b;
cout << endl << "Enter the number of rows in Matrix 2 : ";
cin >> c;
cout << endl << "Enter the number of column in Matrix 2 : "; …Run Code Online (Sandbox Code Playgroud) 鉴于以下代码:
#include <string>
#include <sstream>
class SomeClass {
public:
SomeClass& operator<<(long value) { return *this; }
SomeClass& operator<<(unsigned long value) { return *this; }
SomeClass& operator<<(float value) { return *this; }
SomeClass& operator<<(double value) { return *this; }
SomeClass& operator<<(long double value) { return *this; }
SomeClass& operator<<(bool value);
{ return *this; }
SomeClass& operator<<(const void* value);
{ return *this; }
SomeClass& operator<<(::std::string aString) { return *this; }
};
int main() {
SomeClass aClass;
std::ostringstream aStream;
aClass << 2;
aClass …Run Code Online (Sandbox Code Playgroud) 当我尝试检查是否str[index]等于我得到异常字符串超出范围时
std::string Test::getTheText(std::string str) {
int index = 7;
string text;
cout << str[index] << endl; // Work!!
while (str[index] != '\"') // Exception,why?? also try while(str[index]!=34)
text += str[index++];
return text;
}
Run Code Online (Sandbox Code Playgroud)
我的字符串是:文本– bla bla
Scanf应该返回输入的字符数... 但奇怪的是一直只返回1.
scanf ("%d",&num_test_cases);
for (i=0;i<num_test_cases;i++)
{
level=scanf ("%s",ch);
printf ("\n %s\n",ch);
printf ("%lld\n",level);
}
Run Code Online (Sandbox Code Playgroud)
样本输入:
4
lrl
rll
r
lllr
Run Code Online (Sandbox Code Playgroud)
输出:
lrl
1
rll
1
r
1
lllr
1
Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <stdio.h>
#include <GL/glut.h>
#include <windows.h>
#include <math.h>
using namespace std;
void display(void) {
float cx = 200, cy = 200, rad = 50;
float startx = cx - rad;
float starty = cy;
int x = startx, y = starty;
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POINTS);
glVertex2f((x - 250.f) / 250.f, (250.f - y) / 250.f);
while (x < cx + rad) {
x++;
y = cy - sqrt(rad * rad - (x - cx) * (x - cx)); …Run Code Online (Sandbox Code Playgroud) 我是opencl domian的新手.我已阅读了一些书籍并尝试编译以下代码
#define __CL_ENABLE_EXCEPTIONS
#define __NO_STD_VECTOR
#define PROGRAM_FILE "blank.cl"
#define KERNEL_FUNC "blank"
//#define __MAX_DEFAULT_VECTOR_SIZE 100
#include <cstdio>
#include <fstream>
#include <iostream>
#include <iterator>
#ifdef Windows
#include <OpenCL/cl.hpp>
#else
#include <CL/cl.hpp>
#endif
using namespace std;
using namespace cl;
int main() {
// int n = 10;
vector<Platform> platforms;
vector<Device> devices;
try {
} catch (exception e) {
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但它给了我很多错误.
其中大多数如下
Error 14 error C4996: 'cl::vector<char *,10>': was declared deprecated C:\Program Files (x86)\AMD APP SDK\2.9\include\CL\cl.hpp 1138 1 Matrix_multilpy_C
Run Code Online (Sandbox Code Playgroud)
那么任何人都可以帮助我.我使用visual …
我无法获取以char数组的形式存储的字符串部分.
char code1 [12]={0};
char c;
string compressed_file;
Run Code Online (Sandbox Code Playgroud)
我从文本文件中获取输入,直到出现','.
cout<<"Input compressed_file name"<<endl;
cin>>compressed_file;
string extracted_file;
cout<<"Input extracted_file name"<<endl;
cin>>extracted_file;
ifstream input;
input.open(compressed_file.c_str());
ofstream decompresscode;
decompresscode.open(extracted_file.c_str());
input>>c;
while(c != ',')
{
int i=0;
code1[i]=c;
cout<<code1[i];
i++;
input>>c;
}
int old=atoi(code1);
cout<<old;
Run Code Online (Sandbox Code Playgroud)
在这里打印code1的值后,我只得到数组的第一个字母.我code1是66,它只打印6.
我正在编写一个程序,允许用户输入要读取的人员数据库的文件名; 然后,程序在每个状态链接中创建状态对象的链接列表和人员对象的链接列表,以组织文件中的信息.
我知道链表部分有效,因为我能够直接在文件名中编码并打印出状态列表和每个州的人员列表; 但是,当我尝试允许用户输入文件名作为命令时,我收到一个总线错误.当我在gdb中运行代码时,它告诉我的是:
Program received signal SIGBUS, Bus error.
0x280df0bd in std::operator>><char, std::char_traits<char> > ()
from /usr/lib/libstdc++.so.5
Run Code Online (Sandbox Code Playgroud)
我甚至没有得到一个行号!任何帮助将非常感激.这是我的代码的命令和读取部分:
List<State*>* read(char* filename) {
string fname, lname, birthday, state;
int ssn;
List<State*>* state_list = new List<State*>();
ifstream file(filename);
if (file.fail()) {
cerr << "Error reading file.\n";
exit(1);
}
while (!file.eof()) {
file >> birthday >> ssn >> fname >> lname >> state;
Link<State*>* searchres = searchList(state, state_list);
Person* p = new Person(fname, lname, ssn, birthday, state);
if (searchres == NULL) // …Run Code Online (Sandbox Code Playgroud) c++ ×8
c ×3
c++11 ×2
gcc ×2
pointers ×2
visual-c++ ×2
ambiguity ×1
arrays ×1
bus-error ×1
char ×1
coredump ×1
graphics ×1
int ×1
linked-list ×1
opencl ×1
opengl ×1
overloading ×1
return-value ×1
scanf ×1
stdvector ×1