我试图在字符串中找到i的数量.这是我的代码:
string str = "CS445isaninterestingcourse";
int num = 0;
for (int i = 0; i < str.length(); i++)
{
if (str.substr(i, i + 1) == 'i')
num++;
}
Run Code Online (Sandbox Code Playgroud)
但我得到错误.有人可以帮忙吗?
谢谢.
我发现一篇文章说,删除矢量项的最佳方法是使用擦除 - 删除习语,如下所示
//say vec = 1963,1923,1900,1963,1967;
vector.erase(std::remove(vec.begin(),vec.end(),1963),vec.end());
Run Code Online (Sandbox Code Playgroud)
根据我的理解,vec.erase在第一个和最后一个迭代器中包含两个参数.范围包括第一个和最后一个之间的所有元素.我想知道如果要删除的值存在于向量中的不同索引处,std :: remove将如何给出单个迭代器范围.我想知道std :: remove如何与vector.erase一起使用
为什么std::unique_ptr包含删除函数签名作为模板定义的一部分有什么原因?
template<class T, class Deleter = std::default_delete<T>> class unique_ptr;
Run Code Online (Sandbox Code Playgroud)
VS
template< class T > class shared_ptr;
Run Code Online (Sandbox Code Playgroud) 在访问向量元素时,使用at和括号之间是否有任何区别[]?
我看到同样的人喜欢myvector.at()上面myvector[],是否有一些东西使它更优选,如在某些情况下更好的表现,或与其他一些语言共同?
我知道参数的默认参数值不能是另一个参数的值.例如
void foo( int a, int b = a );
Run Code Online (Sandbox Code Playgroud)
显然,b的默认值不是编译时常量,但在我看来,编译器可以生成代码来执行此操作.
我敢肯定有一个很好的理由不允许这个,我想知道它是什么?
我正在尝试创建一个代码,通过csv数据库解析库存信息.目前,我已生成代码,以便它将使用关键字进行搜索并打印整行,但我正在尝试获取它,以便以整齐格式化的方式打印带有标题行的整行.
我试图得到它,以便如果我搜索谷歌,它会回来
Symbol GOOG
NAME Google Inc
今日高位$ 568.77
csv如何看起来像:
符号,名称,价格,今日高点,今日低点,52周低点
Google,Google Inc.,$ 568.77,$ 570.25,$ 560.35
AAPL,Apple Inc.,$ 93.28,$ 63.89,$ 99.44.
码:
string NameSearch::getInput()
{
cout << "Enter the name of the company you would like to search for: ";
getline(cin, input);
return input;
}
void NameSearch::NameAlgorithm()
{
string line;
ifstream fs("Stock Database.csv");
while (!fs.eof())
{
getline(fs, line);
string companyname = "";
string a;
int column = 1;
int commacount = 0;
int ChrCount = 0;
while (line != "\0") …Run Code Online (Sandbox Code Playgroud) 考虑这两个功能.
int foo(const int& a) {
return a + 5;
}
int bar(const int a) {
return a + 5;
}
Run Code Online (Sandbox Code Playgroud)
给定这些函数,g ++ - 4.9在完全优化时生成以下代码.
Run Code Online (Sandbox Code Playgroud)g++-4.9 -std=c++11 -Ofast -march=native -DNDEBUG -fno-exceptions -Wall -S -o test17.S test17.cpp
# foo
movl (%rdi), %eax
addl $5, %eax
ret
# bar
leal 5(%rdi), %eax
ret
Run Code Online (Sandbox Code Playgroud)
这台机器是x86-64 Mac.
为什么编译器在这里"文字化"?
我的理解是,C++中的引用通常是作为指针实现的,但这并不是标准规定的.它们最好被理解为变量的别名.详细信息留待实施.
鉴于引用实际上不需要指针,为两个函数生成相同的代码似乎是一个明显的优化.生成这const int两个函数的代码是否合法?这两个函数之间是否存在可观察到的差异?
在我的第一个小项目中,我是C++的新手让我头疼.
如果我理解正确的pbolem,我需要cout区间[a,b]的总和,这应该意味着:17 + 18 + 19 + 20 + 21 ..... + 52 =?(纠正我,如果我错了!)我尝试了while,do-while并且他们都结束了无限循环所以现在我尝试for循环让我只增加a的值直到它达到52.
Run Code Online (Sandbox Code Playgroud)#include <iostream> int main(int argc, char* argv[]) { const int a = 17; const int b = 52; int summe = 0; for(summe = a; summe <=b; summe++) std::cout << "Summe: " << summe << "\n"; return 0; }
最近我做了一个程序,它有一个字符数组 board[8][8][2];
它基本上是一个8X8板,可以存储'2'字母字符串.我没有提供完整的代码.
但这是问题所在.
for (j = 0; j < 8; j++) {
strcpy(board[1][j], P[j].sym);
}
cout << board[1][1] << endl;
Run Code Online (Sandbox Code Playgroud)
这里P[1].sym="P1"与P[0].sym="P0"和P[2].sym="P2"
因此P[j].sym基本上是一个两个字母的字符串,board[1][j]也应该是一个两个字母的字符串.
但输出为
cout << board[1][1] << endl;
Run Code Online (Sandbox Code Playgroud)
给出为 P1P2P3P4P5P6P7
和输出
cout << board[1][0] << endl;
Run Code Online (Sandbox Code Playgroud)
给出为 P0P1P2P3P4P5P6P7
对于
cout << board[1][5] << endl;
Run Code Online (Sandbox Code Playgroud)
P5P6P7 是输出.
为了消除任何疑问,整个board[8][8][[2]已经初始化并且所有P[j].sym已经初始化.
如果它有帮助,这里是初始化的代码P:
#include <iostream>
#include <string.h>
using namespace std;
class Game
{
public:
char board[8][8][2];
char *****possibilities;
}; …Run Code Online (Sandbox Code Playgroud) 我正在做CS50的凯撒问题集,当我尝试移动大写字母时,if (isupper(argument) == true)用来检查我想要移动的字符是否为大写,它没有用,它认为大写字母实际上不是大写。当我将其切换到 时if (isupper(argument)),程序正确地移动了大写字母。这两种格式有什么区别吗?这是我使用的代码(我指的是 for 循环中的代码):
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
//Check wether there is only 1 command line argument
if (argc == 2)
{
//Check if there is any character that's not a digit
for (int i = 0; i < strlen(argv[1]); i++)
{
if (isdigit(argv[1][i]) == false)
{
printf("Usage: ./caesar key\n");
return 1;
}
}
}
else
{
printf("Usage: ./caesar key\n"); …Run Code Online (Sandbox Code Playgroud)