小编waz*_*eer的帖子

为什么打印字符串数组输出十六进制?

为什么以下程序将"0x2ffee4"打印到控制台?

#include <string>
#include <iostream>

using namespace std;

int main() {
    string city1[] = "toronto";
    cout << city1;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ string c++11

7
推荐指数
2
解决办法
1131
查看次数

为什么这个unsigned int持有的数据多于内存数量?

无符号int只能容纳32位数据.当我为它分配一个比它能容纳的值更大的值时,为什么编译器不会给出错误?

我尝试了其他各种值,但仍然没有错误.

int main()
{
    unsigned int mem = 0x89678456543454345934;
    cout << mem;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ memory allocation

6
推荐指数
1
解决办法
185
查看次数

for循环只打印一半的值(使用向量)

main中的for循环假设pop_back值只打印数组中值的一半.但是,当我写(i <=用户)时,它会打印所有值.

MyVector.h

template<class T>
T MyVector<T>::Pop_back( ){
   return elements_ptr[--vectorSize];
   }
Run Code Online (Sandbox Code Playgroud)

main.cpp中

int main() 
{
    MyVector<int> v1;
    int user = 500;

    for(int i= 1; i <= user; i++){
         v1.Push_back(i);
    }
    cout << v1.size() << endl; // outputs 500

    for (int j = 0; j < v1.size(); j++){
         cout << v1.Pop_back() << " ";
         if( j % 20 ==0 ){
         cout << endl;
         }
   }

   return 0;
 }
Run Code Online (Sandbox Code Playgroud)

c++ memory vector dynamic

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

为什么以下代码崩溃.(动态记忆)?

我的程序计算字符串中每个数字从0到9的出现次数.在我看来,我已经完成了所有事情,但问题仍然存在.

int main(){
    string word = "23456745";
    int* ReturnArray = count(word);

    for(int i =0; i < 10; i++){
        cout << i << ": " <<  ReturnArray[i] << " \n";
    }

    delete [] ReturnArray;

    return 0;
}

int* count(const string& s){
    int length = s.length();
    int* array = new int(10);
    int counter =0;

    for(int j = 0 ; j < 10 ; j++) {
        for(int i = 0 ; i < length ; i++) {
            char character = s[i];
            int value …
Run Code Online (Sandbox Code Playgroud)

c++ memory dynamic

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

标签 统计

c++ ×4

memory ×3

dynamic ×2

allocation ×1

c++11 ×1

string ×1

vector ×1