在C++ 11中,我可以迭代一些容器,如下所示:
for(auto i : vec){
std::cout << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但我知道这是不必要的 - 不必要的,因为我只需要打印 -的值vec- 制作(编辑:每个元素)的副本vec,所以我可以这样做:
for(auto &i : vec){
std::cout << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是我想确保它们的值vec永远不会被修改并遵守const-correctness,所以我可以这样做:
for(const auto &i : vec){
std::cout << i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:如果我只需要查看某个容器的值,那么最后的循环(const auto &i)是否总是首选,因为没有额外副本(编辑:每个元素)的效率提高vec?
我正在开发一个我正在开发的程序,我正在考虑在整个过程中进行这种改变,因为效率是至关重要的(我在第一时间使用C++的原因).
我有以下代码来制作unordered_set<Interval>.编译好了.
struct Interval {
unsigned int begin;
unsigned int end;
bool updated; //true if concat. initially false
int patternIndex; //pattern index. valid for single pattern
int proteinIndex; //protein index. for retrieving the pattern
};
struct Hash {
size_t operator()(const Interval &interval);
};
size_t Hash::operator()(const Interval &interval){
string temp = to_string(interval.begin) + to_string(interval.end) + to_string(interval.proteinIndex);
return hash<string>()(temp);
}
unordered_set<Interval, string, Hash> test;
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用此代码插入时,我无法编译:
for(list<Interval>::iterator i = concat.begin(); i != concat.end(); ++i){
test.insert((*i));
}
Run Code Online (Sandbox Code Playgroud)
而且,我无法确定错误消息的问题,例如:
note: candidate is:
note: size_t …Run Code Online (Sandbox Code Playgroud) 我有一些带有malloc语句的C代码,我想与一些C++代码合并.
我想知道什么时候以及为什么要在C++中使用malloc neccessary进行类型转换?
例如:
char *str = (char*)malloc(strlen(argv[1]) * sizeof(char));
Run Code Online (Sandbox Code Playgroud) 我创建了一个unordered_set我自己类型的struct. 我有一个iterator该集合,并希望增加该集合count的成员 ( ) 所指向的。但是,编译器会抱怨以下消息:structiterator
main.cpp:61:18: error: increment of member \xe2\x80\x98SentimentWord::count\xe2\x80\x99 in read-only object
我怎样才能解决这个问题?
\n\n这是我的代码:
\n\n#include <fstream>\n#include <iostream>\n#include <cstdlib> \n#include <string>\n#include <unordered_set>\n\n\nusing namespace std;\n\n\nstruct SentimentWord {\n string word;\n int count;\n};\n\n\n//hash function and equality definition - needed to used unordered_set with type SentimentWord\nstruct SentimentWordHash {\n size_t operator () (const SentimentWord &sw) const;\n};\n\nbool operator == (SentimentWord const &lhs, SentimentWord const &rhs);\n\n\n\nint main(int argc, char **argv){\n\n\n ifstream fin;\n …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序来读取大约30,000个文件中的单词,但是在第4092次迭代之后我再也无法读取新文件了.我甚至尝试在循环结束时包含一个free(filePointer),但在尝试打开4092nd文件后,我的文件指针仍为NULL.任何想法或建议将不胜感激.
int main(int argc, char *argv[]) {
hashTable *bigramHT = create_table(100000000);
hashTable *tokenHT = create_table(100000000);
int numTokens = 0;
/* file pointer */
FILE *fp = NULL;
/* directory pointer */
DIR *dirp = NULL;
/* pointer to directory struct */
struct dirent *entry = NULL;
/* check if directory exists and can be opened */
if((dirp = opendir(argv[1])) == NULL){
return 1;
}
/* allocate memory for the filename paths (using maximum possible length) */
char *path;
path = …Run Code Online (Sandbox Code Playgroud)