为什么内存显示和输出显示是倒过来的



```c++
     张              三        \0
d5    c5       c8        fd        00      00

张三在内存中是这么显示的,为什么输出后却是倒过来的呢
张:0xc5d5

```

字符串的顺序是从左到右,数据储存的时候是低位在前高位在后,也就是从右到左排序。比如你输入app三个字符,你看到的是a--p--p这个顺序,实际上在内存里边,是从右到左排放的,也就是P-P-a的排放方式。

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/733728
  • 这篇博客你也可以参考下:浏览器控制台过滤无用的提示和报错
  • 除此之外, 这篇博客: 输出目标串在模式串出现的所有位置中的 示例: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 目标串:123abc456abc789abc
    模式串: abc
    输入:3 9 15

    大致思路:
    首先通过KMP算法判断目标串中是否含有模式串,如果不含有的话,不用进行后面的判断,直接输出-1
    若含有模式串,则需要将模式串出现的所有位置输出。方法是通过截取字符串,相当于将在目标串中已经判断过的位置剪去,例如目标串:123abc456abc789abc 模式串: abc,第一次匹配出现位置为3,tmp=456abc789abc,然后将tmp作为目标串接着匹配,知道目标串中没有模式串或者目标串的长度小于模式串。

    #include<iostream>
    #include<string>
    #include<vector>
    using namespace std;
    
    
    int kmp(string s, string p, int next[])
    {
    	int s_len = s.size();
    	int p_len = p.size();
    
    	int i = 0, j = 0;
    	while (i < p_len && j < s_len)
    	{
    		if (p[i] == s[j])
    		{
    			i++;
    			j++;
    		}
    		else
    		{
    			if (i == 0)
    				j++;
    			else
    				i = next[i - 1] + 1;
    		}
    	}
    
    	if (i < p_len)
    		return -1;
    	return j - p_len;
    }
    
    void getnext(string p, int next[])
    {
    	int len = p.size();
    	next[0] = -1;
    	int i, j;
    	for (j = 1; j < len; j++)
    	{
    		i = next[j - 1];
    		while (p[j] != p[i + 1] && i >= 0)
    			i = next[i];
    		if (p[j] == p[i + 1])
    			next[j] = i + 1;
    		else
    			next[j] = -1;
    	}
    }
    
    int main()
    {
    	string pMain;
    	string pPattern;
    	cout << "请输入目标串:" << endl;
    	cin >> pMain;
    	cout << "请输入模式串:" << endl;
    	cin >> pPattern;
    	vector<int> ans;
    	int* next = new int[pPattern.size()];
    	getnext(pPattern, next);
    	if (kmp(pMain, pPattern, next) == -1)
    	{
    		cout << kmp(pMain, pPattern, next);
    	}
    	else
    	{
    		ans.push_back(kmp(pMain, pPattern, next));
    		string ppmain = pMain;
    		while (true)
    		{
    			string tmp = "";
    			for (int i = ans[ans.size() - 1] + pPattern.size(); i < ppmain.size(); i++)
    			{
    				tmp += ppmain[i];
    			}
    			if (tmp.size() < pPattern.size())
    				break;
    			int answer = kmp(tmp, pPattern, next);
    			if (answer == -1)
    				break;
    			else
    			{
    				answer += ans[ans.size() - 1];
    				answer += +pPattern.size();
    				ans.push_back(answer);
    			}
    		}
    	}
    	for (int i = 0; i < ans.size(); i++)
    	{
    		if (i != 0)
    			cout << "\t";
    		cout << ans[i];
    	}
    	return 0;
    }
    

    在这里插入图片描述